无法运行查询:??没有查询错误消息导致我无处可去

时间:2012-10-03 15:10:14

标签: php mysql authentication login

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

这是我的代码:

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>";   

3 个答案:

答案 0 :(得分:2)

您的问题似乎是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。

答案 1 :(得分:0)

$numrows = mysql_num_rows($query) or die ('Unable to run query:'.mysql_error());

如果您的查询返回0行,那么第一部分将评估为false。然后,这将触发die()

答案 2 :(得分:0)

如果要返回任何结果集,

将此行if($numrows == 1){更改为if( $numrows !== FALSE && $numrows >= 1 ){,因为您希望通过条件检查。

或者,如果总是只返回1条记录,那么if( $numrows !== FALSE && $numrows === 1 ){