PHP登录错误:[重复](PHP Login error: [duplicate])

编程入门 行业动态 更新时间:2024-10-25 00:27:55
PHP登录错误:[重复](PHP Login error: [duplicate])

这个问题在这里已有答案:

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 answers

Im 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.

更多推荐

本文发布于:2023-07-08 19:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   PHP   Login   duplicate   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!