views:

36

answers:

5

Hi. 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

+5  A: 

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
+1  A: 

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
+1  A: 
echo mysql_num_rows($query);
Marwelln
A: 
$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
A: 

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.

Col. Shrapnel
Condescension is unnecessary and counterproductive.
Amber