-3

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));
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Ethan Webster
  • 103
  • 3
  • 3
  • 10
  • Look to the right. Pick any one. This has been answered _many, many times_ before. – Lightness Races in Orbit Nov 04 '11 at 17:39
  • Seems like we're seeing more and more of these. At least 2-3/day over the last week. – Michael Berkowski Nov 04 '11 at 17:40
  • and http://stackoverflow.com/questions/3742239/php-mysql-error-warning-mysql-num-rows-expects-parameter-1-to-be-resourc and http://stackoverflow.com/questions/5428575/how-to-fix-this-warning-mysql-num-rows-expects-parameter-1-to-be-resource and .... ad nauseum – Marc B Nov 04 '11 at 17:40
  • @Michael: Just more of the trend of Stack Overflow turning from "questions about programming languages" repository into some "fix my code please" discussion board. I think the concensus thus far has been, unfortunately, that all we can do about it is to continue wasting our time downvoting and closevoting. – Lightness Races in Orbit Nov 04 '11 at 17:41
  • I can't seem to find anything that helps. – Ethan Webster Nov 04 '11 at 17:44
  • @user921489: Every single answer to every single duplicate question under "Related" helps. If you need clarification, write a comment... when you have gained enough rep through site participation to do so. – Lightness Races in Orbit Nov 04 '11 at 17:48

2 Answers2

4

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.

Randell
  • 6,112
  • 6
  • 45
  • 70
Oroboros102
  • 2,214
  • 1
  • 27
  • 41
0

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

Andri
  • 1,503
  • 13
  • 20