SQLSRV_NUM_ROWS error

2019-07-23 14:19发布

问题:

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!

回答1:

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

write like this: username='$user' .

without '' at username



标签: php sqlsrv