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));

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.