Warning: mysql_num_rows() expects parameter 1 to b

2019-09-20 06:59发布

I've just changed a site over to a new server, and now I'm getting an error with this code:

<?php 
    $result = mysql_query("SELECT * FROM trends ");
    $num_rows = mysql_num_rows($result);
    echo "<strong>" . $num_rows . "</strong>";
?>

The error is:

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

How can I fix this?

Cheers.

1条回答
Evening l夕情丶
2楼-- · 2019-09-20 07:15

Your mysql_query return false if it fails.

"For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error." (http://php.net/manual/en/function.mysql-query.php)

Therefore you need to check if the query failed (which is the case here) before using the result.

查看更多
登录 后发表回答