php - 无法运行查询 : ??没有查询错误消息让我无处可去

标签 php mysql authentication

所以我有这个验证码。除了我输入错误的用户名外,其他一切都有效。它只是一直显示在页面中:“无法运行查询:” <-- 只有这个。我不知道出了什么问题。请帮忙。

这是我的那部分代码:

if($UserN){
    if($UserP){
        require("connect.php");

        $query = mysql_query("SELECT * FROM customer WHERE UserName = '$UserN'");
        $numrows = mysql_num_rows($query) or die ('Unable to run query:'.mysql_error());

        if($numrows == 1){
            $row = mysql_fetch_assoc($query)or die ('Unable to run query:'.mysql_error()); // fetch associated: get function from a query for a database
            $dbpass = $row['PassWord']; // read password of inputted user from the query.
            $dbuser = $row['UserName']; // read username from the query
            $dbactive = $row['Active']; // read if user is active
            $dbid = $row['CustNum'];

            if($UserP == $dbpass){
                if($dbactive == 1){
                //set session information
                $_SESSION['CustNum'] = $dbid;
                $_SESSION['UserName'] = $dbuser;

                echo "<br><br><center><b>You have been logged in as <b>$dbuser</b>. <a href='orderform.php'>Click here</a> to go to the member page.</b></center><br><br>";
                }
                else
                    echo "$form <center><b>You need to activate your account.</b></center></br>";
            }
            else
                echo "$form <center><b>You did not enter a correct password.</b></center> </br>";
        }
        else
            echo "$form <center><b>The username you entered was not found.</b></center></br> ";

        mysql_close();
    }
    else
        echo "$form <center><b>You must enter your password. </b></center></br>";
}
else
    echo "$form <center><b>You must enter your username. </b></center></br>";   

最佳答案

您的问题似乎是 mysql_num_rows() 在用户名无效的情况下返回 0。

您可以像这样调整您的代码:

$numrows = mysql_num_rows($query);
if ($numrows === FALSE)
    die ('Unable to run query:'.mysql_error());
if ($numrows === 0) {
    echo "User not found!";
}

注意使用 === 来测试 mysql_num_rows() 是否返回 0 或 FALSE。

关于php - 无法运行查询 : ??没有查询错误消息让我无处可去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12711446/

相关文章:

php - 迁移 WordPress 网站后我无法访问管理员(白页)

php - PDO 使用 BLOB(从表中获取图像)

api - 使用 Chrome 身份验证 token xoxc- 访问 Slack API

cocoa - 如何在我的安全首选项 Pane 中创建锁定/解锁按钮和行为?

mysql - 使用 LDAP/AD 登录身份验证时,如果使用 AD 记录不存在用户行,如何创建用户行

php - 带有子记录的重复 MYSQL 记录

java - 如何同时使用 Tomcat 的非阻塞连接器(NIO 或 APR)和 Apache Httpd?

php - 无法使用pdo和json访问php登录

Mysql where 子句大写字母

php - 为什么 while 循环显示单行?