SQLSRV_NUM_ROWS error

2019-07-23 14:01发布

I have a PHP application for which I have tried creating a login script. When I try logging in I get the following error message:

Warning: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given

It also shows 'Invalid Credentials' even though the username and password are correct. My code is as follows:

if(isset($_POST['submit'])){
    $user = $_POST['u'];
    $pass = $_POST['p'];

    //database connection
    $serverName = "localhost\SQLEXPRESS";
    $connectionInfo = array( "Database"=>"AuServer", "ReturnDatesAsStrings"=>"true");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    //query
    $sql = "SELECT *  FROM `users` WHERE `username` = '$user' AND `password` = '$pass'";
    $params = array();
    $options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
    $stmt = sqlsrv_query( $conn, $sql , $params, $options );

    $row_count = sqlsrv_num_rows( $stmt );

    echo $row_count;

    if ($row_count > 0){
        session_start();
        $_SESSION['auth'] = "Yes";
        header('Location: counterpage.php');
    }

    else
        echo "Invalid Credentials";

}

Any help will be much appreciated!

标签: php sqlsrv
1条回答
Anthone
2楼-- · 2019-07-23 14:27

don't use 'username' = '$user'.

write like this: username='$user' .

without '' at username

查看更多
登录 后发表回答