'Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given [duplicate]

I get this error whenever I run this:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

The Code:

$amn = mysql_query("SELECT * FROM `Messages` WHERE to_user='$usr' AND read='0'");
print_r(mysql_num_rows($amn));


Solution 1:[1]

That's because mysql_query sometimes returns boolean false (query error). You need to check it:

$amn = mysql_query("SELECT * FROM `Messages` WHERE to_user='$usr' AND read='0'");

if($amn === false) {
    var_dump(mysql_error());
}
else {
    print_r(mysql_num_rows($amn));
}

Code above is written in bad style and deprecated. Use PDO with Exceptions in real projects.

Solution 2:[2]

I would guess your mysql_query is returning false, probably because of the weird quotes on the "Messages" bit on your query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Randell
Solution 2 Andri