开发者

mysql_num_rows for an mysql object?

How do I find mysql_num_rows for an object.

This gives an error:

$query开发者_JAVA技巧 = 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


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);


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:

  • http://php.net/manual/en/function.mysql-num-rows.php


echo mysql_num_rows($query);


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜