1

Can someone help me to solve this error:

mysql_num_rows() expects parameter 1 to be resource

This is my code:

$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
JessieNBarnes
  • 53
  • 1
  • 1
  • 7
  • Try: $query = mysql_query($query) or die(mysql_error()); And post result of error. This is issue with query which might be invalid. – Piotr Oct 16 '13 at 13:46

3 Answers3

1

Rename your variable names:

$result = mysql_query($query);
$numrows = mysql_num_rows($result);
if ($numrows > 0){
VancleiP
  • 657
  • 4
  • 7
  • This is not issue here! $query = mysql_query($query); Above code is 100% correct – Piotr Oct 16 '13 at 13:45
  • The php documentation says you must have a result as parameter. He was passing the same variable name for the query and parameter (http://php.net/manual/en/function.mysql-num-rows.php) – VancleiP Oct 16 '13 at 13:47
  • 1
    In case you don't know... $query variable is changed in that line from string to result type. PHP works that way - thx to that you can do for example $i=1;$i=$i+1; and result will be 2... – Piotr Oct 16 '13 at 13:48
  • Yes, you're right ;-) but it's better do the correct way: one variable for each thing, or it'll lead to confusion later – VancleiP Oct 16 '13 at 13:49
  • It is good practice to assign different variables, but correct code is correct... so renaming variables here won't change a thing. It's query issue. I bet it is just SQL error. – Piotr Oct 16 '13 at 13:51
  • But the error is "mysql_num_rows() expects parameter 1 to be resource", so it seems it is setting a non-resource variable (???). – VancleiP Oct 16 '13 at 13:52
  • 1
    @VancleiP - that's the error you get when your query fails. In that case, `mysql_query` returns a boolean false. – andrewsi Oct 16 '13 at 13:53
0

You may try this:-

$numrows = $query->num_rows;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • This would supress warning, but would not work as expected. It won't return number of rows correctly - in fact it will return nothing. – Piotr Oct 16 '13 at 14:10
0

Try out this one:

$result = mysql_query($query);

if (!$result) {
   die('Invalid query: ' . mysql_error());
}
else {
   $numrows = mysql_num_rows($result);
      if ($numrows > 0){
      // put your code here
      }
   }
RawBean
  • 442
  • 5
  • 20