php 警告:mysql_num_rows() 期望参数 1 是资源,布尔值给定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8013526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given
提问by Ethan Webster
I get this error whenever I run this:
每当我运行这个时,我都会收到此错误:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given
The Code:
编码:
$amn = mysql_query("SELECT * FROM `Messages` WHERE to_user='$usr' AND read='0'");
print_r(mysql_num_rows($amn));
回答by Oroboros102
That's because mysql_query sometimes returns boolean false (query error). You need to check it:
那是因为 mysql_query 有时会返回布尔值 false(查询错误)。你需要检查一下:
$amn = mysql_query("SELECT * FROM `Messages` WHERE to_user='$usr' AND read='0'");
if($amn === false) {
var_dump(mysql_error());
}
else {
print_r(mysql_num_rows($amn));
}
Code above is written in bad style and deprecated. Use PDO with Exceptions in real projects.
上面的代码写得不好,不推荐使用。在实际项目中使用 PDO with Exceptions。
回答by Andri
I would guess your mysql_query is returning false, probably because of the weird quotes on the "Messages" bit on your query.
我猜您的 mysql_query 返回 false,可能是因为您查询的“消息”位上有奇怪的引号。