php 警告:mysql_num_rows() 期望参数 1 是资源,布尔值在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21516255/
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 in?
提问by Mel
I'm trying to make a log in system using PHP but when I purposely attempt to log in with incorrect credentials this error message appears:
我正在尝试使用 PHP 登录系统,但是当我故意尝试使用不正确的凭据登录时,会出现此错误消息:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ...
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ...
The message I want to echo when someone types in the wrong credentials appears, but it appears below the warning message. These are the lines of code associated with the line of code causing the warning message to appear:
当有人输入错误的凭据时,我想回显的消息出现,但它出现在警告消息下方。这些是与导致警告消息出现的代码行相关联的代码行:
$query = mysql_query("SELECT * FROM users WHERE username='$user'");
if ($numrows == 1){
$row = mysql_fetch_assoc($query);
$dbid = $row['id'];
$dbuser = $row['username'];
$dbpass = $row['password'];
$dbactive = $row['active'];
And here is the actual line of code itself:
这是实际的代码行本身:
$numrows = mysql_num_rows($query);
I may need to edit the question to add more lines of code, please tell me if I do. Also this question may be a possible duplicate of this post: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given inbut I'm not too sure though. But please tell me what could be causing this warning message to appear.
我可能需要编辑问题以添加更多代码行,如果我这样做,请告诉我。此外,这个问题可能是这篇文章的可能重复:警告:mysql_fetch_array() 期望参数 1 是资源,布尔值给出,但我不太确定。但请告诉我是什么原因导致出现此警告消息。
回答by Jake N
If mysql_queryreturns false, this is boolean, so you need to check it first before passing it on to mysql_num_rows
如果mysql_query返回 false,这是布尔值,因此您需要先检查它,然后再将其传递给mysql_num_rows
$query = mysql_query("SELECT * FROM users WHERE username='$user'");
if($query)
$numrows = mysql_num_rows($query);
else
die("something failed");

