苦苦于简单的php错误 - mysqli_num_rows()需要登录系统的参数

时间:2015-08-12 11:07:53

标签: php mysqli

我知道有一些与错误消息相关的帖子“mysqli_num_rows()期望参数1为mysqli_result,boolean given”。但是,我仍然无法理解为什么我的脚本无法运行... grr这个错误对我的脚本意味着什么?我该如何纠正呢?我是相对较新的PHP所以请耐心等待我。我正在尝试设计一个简单的登录脚本。

这是我的文件cwk_login_tools。

<?php 
    #Makes Logging in Possible by checking for errors
    # Connect  to database or quit
    $dbc = @mysqli_connect ( 'localhost', 'root', '', 'cswk_db' ) OR die ( mysqli_connect_error() ) ;

    function load(){
        header("http://localhost/SecondCswkAttempt/2017%20Web%20Scenario/cwk_index.php");
        exit();
    }

    #Function to check username  and password
    function validate( $dbc, $username = '' , $pwd = '')
    {
        #Initialise errors array
        $errors = array() ;

        #Check a username has been entered
        if ( empty( $username ) ) {
            $errors[] = 'Enter your username .' ;
        } else {
        $u = mysqli_real_escape_string( $dbc , trim( $username) );  
        }

        #Check password has been entered
        if ( empty( $pwd) ) {
            $errors[] = 'Enter your password.' ;
        } else {
        $p = mysqli_real_escape_string( $dbc , trim( $pwd) );   
        }

        #On success retrieve user_id, and username from  the 'users' database
        if ( empty( $errors) ) {
            $q = "SELECT user_id, username, password, FROM users WHERE username = '$u' AND pass = '$p' " ;
            $r = mysqli_query ($dbc, $q ) ;

            #Check password field
            if ( mysqli_num_rows( $r ) == 1 ) {
                $row = mysqli_fetch_array ( $r , MYSQLI_ASSOC) ;
                return array(true, $row ) ;
            }
            #Or on failure set error message
        else {
            $errors[] = 'username  and password not found.' ;   }
        #On failure retrieve error message/s
        return array( false, $errors ); 
        }
    }
    ?>

看起来不起作用的是行:

if ( mysqli_num_rows( $r ) == 1 ) {
            $row = mysqli_fetch_array ( $r , MYSQLI_ASSOC) ;
            return array(true, $row ) ;
        }

但是,此文件与另一个文件一起使用以使我的登录系统工作。这是名为cwk_login_action.php的文件:

<?php #PROCESS LOGIN ATTEMPT

#Check form submitted
if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST' )
{   
# Connect  to database or quit
$dbc = @mysqli_connect ( 'localhost', 'root', '', 'cswk_db' ) OR die ( mysqli_connect_error() ) ;

    #Get connection, load and validate functions
    require ('cwk_login_tools.php');

    #Check login
    list ( $check, $data ) = validate ( $dbc, $_POST[ 'username' ] , $_POST[ 'pass' ] );

    #On success set session data and display logged in page
    if ( $check ) {
        session_start();
        $_SESSION[ 'user_id' ] = $data[ 'user_id' ];
        $_SESSION[ 'username' ] = $data[ 'username' ];
        load ( 'home.php') ;
    }
    #Or on failure set errors
    else { $errors = $data; }
    #Close database connection
    mysqli_close( $dbc ) ;
}
#Continue to display login page on failure
include ( 'cwk_login.php')
?>

1 个答案:

答案 0 :(得分:0)

此错误表示MySQL查询无法执行。在这种情况下,它看起来像语法错误:

$q = "SELECT user_id, username, password, FROM users WHERE username = '$u' AND pass = '$p' " ;

“密码”后面有逗号。尝试删除它来修复语法错误。此外,您似乎同时引用“密码”和“传递” - 它在数据库中调用了什么?我想其中一列不存在。