mysql_query()非常奇怪地失败了

This is a really simple thing, but it's not working for some reason. Heres my code.

I am making function (its part of a class) which checks if a username or email exists:

public function exists ($what, $who)
{
    $sql = "SELECT * FROM users WHERE $what = $who";
    $query = mysql_query($sql);

    if (mysql_num_rows($query) != 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

The function returns nothing. In fact if I run that query through regular PHP it returns nothing also. I don't understand why.

This following piece of code returns news entries perfectly:

function fetch($id = '') 
{
    if (empty($id))
    {
        $query = 'SELECT * FROM news ORDER BY id desc';
    }
    elseif (is_numeric($id))
    {
        $query = "SELECT * FROM news WHERE id = $id";
    }
    else
    {
        $route->to(SITE_URL);
    }
    $result = mysql_query($query);

    if (mysql_num_rows($result) > 0)
    {
        return $result;
    }

}

I am confused.

The problem is that you are missing quotes in your query:

 $sql = "SELECT * FROM users WHERE $what = $who";
 //SELECT * FROM users WHERE username = Mario  is not a valid query

should be:

 $sql = "SELECT * FROM users WHERE $what = '$who'";

the other queries are working because you are checking against an id, in this case against a string (and in this case you need quotes)

maybe the query execution failed and you have error turned off on screen in your php.ini

Try to add an intermediate check on the correct execution of the query:

 $query = mysql_query($sql); 

 if ($query === FALSE) {

     // log error with mysql_errno($conn) and mysql_error($conn);

 } else {

     if (mysql_num_rows($query) != 0) {

         return true;

etc. etc.