Warning: mysql_num_rows() expects parameter 1 to be resource boolean given [duplicate]

你离开我真会死。 提交于 2019-12-26 03:54:50

问题


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:


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.



来源:https://stackoverflow.com/questions/26476056/warning-mysql-num-rows-expects-parameter-1-to-be-resource-boolean-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!