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

1

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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:

Comments

1
echo mysql_num_rows($query);

Comments

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.

Comments

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.

1 Comment

Condescension is unnecessary and counterproductive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.