这个问题在这里已有答案:
mysql_fetch_array()/ mysql_fetch_assoc()/ mysql_fetch_row()/ mysql_num_rows等...期望参数1为资源 31答案我试图用php和mysql制作一个登录脚本。 一切都很好,除了我在尝试登录时遇到此错误:
警告:mysql_num_rows()期望参数1是资源,在第10行的/home/deadmin1/public_html/login/index.php中给出布尔值
相关代码是:
<?php require("config.php"); if(isset($_POST['Submit'])){ $userName=getStr($_POST['userName']); $passWord=md5(getStr($_POST['passWord'])); $query=mysql_query("select userID from tbl_users where userName='$userName' and passWord='$passWord' "); if(mysql_num_rows($query)==1){ //login success $data=mysql_fetch_array($query,1); $_SESSION['userID']=$data['userID']; header("location:dashboard.php"); }else{ //login failed $error="Invalid Login"; } }This question already has an answer here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 32 answersIm trying to make a login script with php and mysql. everything is fine except i get this error when trying to login:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/deadmin1/public_html/login/index.php on line 10
the relevant code is:
<?php require("config.php"); if(isset($_POST['Submit'])){ $userName=getStr($_POST['userName']); $passWord=md5(getStr($_POST['passWord'])); $query=mysql_query("select userID from tbl_users where userName='$userName' and passWord='$passWord' "); if(mysql_num_rows($query)==1){ //login success $data=mysql_fetch_array($query,1); $_SESSION['userID']=$data['userID']; header("location:dashboard.php"); }else{ //login failed $error="Invalid Login"; } }最满意答案
错误消息清楚地说明了查询的错误。
mysql_num_rows()期望参数1是资源 , boolean在...中给出
显然$query是一个布尔值而不是一个合适的mysql结果资源。 我的猜测它等于=== false 。 含义:您的查询错误:
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。 [资源]
尝试通过检查mysql_error()和/或转储$ query var来调试查询
$query=mysql_query(....) or die (mysql_error()); or var_dump($query);其他一些评论:
不推荐使用mysql_ *函数: faq 将此查询重写为PDO语句或mysqli_ *并不难
如果你真的想建立一个登录系统,请查看另一个算法,然后是MD5(如SHA256),salting和其他有关用户身份验证的最佳实践。
The error message clearly states what is wrong with your query.
mysql_num_rows() expects parameter 1 to be resource, boolean given in ...
Apparently $query is a boolean instead of a proper mysql result resource. My guess it equals to === false. Meaning: your query is wrong:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. [source]
Try debugging your query by checking mysql_error() and /or dumping the $query var
$query=mysql_query(....) or die (mysql_error()); or var_dump($query);Some other remarks:
the mysql_* functions are deprecated: faq It is not that hard to rewrite this query to a PDO statement or mysqli_*
And if you really want to make a login system, please look into an other algorithm then MD5 (like SHA256), salting and other best practices regarding user authentication.
更多推荐
发布评论