SQLSRV_NUM_ROWS错误

时间:2014-04-04 09:09:10

标签: php sqlsrv

我有一个PHP应用程序,我尝试过创建一个登录脚本。当我尝试登录时,收到以下错误消息:

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

即使用户名和密码正确,它也会显示“凭据无效”。我的代码如下:

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

}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

不要使用'username' = '$user'

这样写:username='$user'

''

时没有username