SQLSRV_NUM_ROWS error

和自甴很熟 提交于 2019-12-13 00:38:12

问题


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



来源:https://stackoverflow.com/questions/22858327/sqlsrv-num-rows-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!