0

How do I find mysql_num_rows for an object.

This gives an error:

$query = mysql_query($sql) or die(mysql_error());   
$row = mysql_fetch_object($query);

echo mysql_num_rows( $row );

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

ganjan
  • 7,356
  • 24
  • 82
  • 133
  • 1
    possible duplicate of [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – thecodeparadox Aug 04 '12 at 09:54

5 Answers5

6

mysql_num_rows expects a result set resource (the set of results returned by mysql_query, ie. what is ending up in your $query variable), not on a single row.

This would work:

$result_set = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result_set);
$row = mysql_fetch_object($result_set);
Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
1

Your should pass the result of mysql_query to mysql_num_rows like this:

echo mysql_num_rows($query);

From Docs:

The result resource that is being evaluated. This result comes from a call to mysql_query().

More Info:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1
echo mysql_num_rows($query);
Marwelln
  • 28,492
  • 21
  • 93
  • 117
0
$query = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($query);

i.e. you need to pass the return value from mysql_query to mysql_num_rows.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
0

Easy. According to manual page, which good programmers always refer to, one object contains one row.
While mysql_num_rows() works with result set resource.

Also I have to say that according to my experience there is very little use if such a function. I can barely find a case where you would need it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345