警告:mysql_num_rows() 期望参数 1 是资源,布尔值在 C:\xampp\htdocs\html\Login\checklogin.php 第 23 行中给出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6567643/
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 C:\xampp\htdocs\html\Login\checklogin.php on line 23
提问by spencer
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Hello im trying to get my login system going and i keep getting this error here is my code:
您好,我正在尝试让我的登录系统正常运行,但我不断收到此错误,这是我的代码:
<?php
$host="localhost";
$username="root";
$password="power1";
$db_name="members";
$tbl_name="users";
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Email=$_POST['Email'];
$Password=$_POST['Password'];
$Email = stripslashes($Email);
$Password = stripslashes($Password);
$Email = mysql_real_escape_string($Email);
$Password = mysql_real_escape_string($Password);
$sql="SELECT * FROM $tbl_name WHERE Email='$Email' AND password ='$Password'";
$result=mysql_query($sql, $link) or die ('Unable to run query:'.mysql_error());
$count=mysql_num_rows($result);
if($count==1){
session_register("Email");
session_register("Password");
header("location:login_success.php");
}
else {
echo "Wrong Email or Password";
}
?>
Its working now
它现在工作
回答by Damien Pirsy
It can be that your query is failing for some reason, try
可能是您的查询由于某种原因失败,请尝试
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
//....
$sql="SELECT * FROM $tbl_name WHERE username='$Email' AND password ='$Password'";
$result=mysql_query($sql, $link) or die ('Unable to run query:'.mysql_error());
or
或者
if(!$result) die ('Unable to run query:'.mysql_error());
When query fails, it returns FALSE, hence the BOOLEAN you are passing to mysql_num_rows(); You should always check if a result actually exists before going on with your code.
当查询失败时,它返回 FALSE,因此你传递给 mysql_num_rows(); 的 BOOLEAN; 在继续执行代码之前,您应该始终检查结果是否确实存在。
回答by jessica
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。
对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等,mysql_query() 在成功时返回 TRUE,在错误时返回 FALSE。
http://php.net/manual/en/function.mysql-query.php
http://php.net/manual/en/function.mysql-query.php
You have an error in your query.
您的查询有误。
回答by Marco
Try this one:
试试这个:
$sql="SELECT * FROM ".$tbl_name." WHERE username='".$Email."' AND password ='".$Password."'";