使用sqlsrv_rows_affected的问题,即使它是真的,它也会一直返回false

时间:2012-02-23 20:57:22

标签: php sql sql-server-2008

我有一个更改用户密码的查询,我想检查用户是否知道他们当前的密码,然后才能更改它。所以它正在工作,我需要在进行更改之前输入当前密码,但我的输出仍然总是假的,这是我的代码:

$passwordnew=$_POST['passwordnew'];
$password=$_POST['password'];


$password_hash = md5($passwordnew);     

$sql = "UPDATE cryptuser  SET password='" . $password_hash."'  WHERE  password = '".md5($password)."' AND userID ='" . $userid ."' ";   

//Check SQL Query       
$stmt = sqlsrv_query( $conn, $sql,array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
    var_dump(sqlsrv_rows_affected($stmt));


    $rows_affected = sqlsrv_rows_affected( $stmt);

    if( $rows_affected === false) {
         die('password incorrect');
    } 

    else{
        echo $rows_affected." the password was updated.<br />";
    }


if(!$stmt) 
{
    die('An error has occured with your registration. If this is an indeliberate occurance, 
         please report this to us through the contact us page with details of the error.');
}

任何人都可以看到这不会输出“密码已更新”,即使它是?它总是返回密码不正确(false)

2 个答案:

答案 0 :(得分:3)

可能与作者不再相关,但对于发现此问题的人,答案是:

sqlsrv_errors()返回一条消息“此函数仅适用于不可滚动的语句”。上面的代码可以通过将SQLSRV_CURSOR_KEYSET更改为SQLSRV_CURSOR_FORWARD或删除'Scrollable'参数来修复。

答案 1 :(得分:0)

Astax是正确的...

这是查询sqlsrv_rows_affected()的完整示例查询。关键是在查询中将sqlsrv_query()$ options设置为array('Scrollable'=> SQLSRV_CURSOR_FORWARD)。如果没有任何参数,只需将array()放入t

    $sql = "UPDATE TOP (1) MyTable SET name=? WHERE id=$id";
    $params = array($name);
    $options =  array('Scrollable' => SQLSRV_CURSOR_FORWARD);
    $stmt = db_mssql_query($db, $sql, $params, $options);

    $updated_rows = sqlsrv_rows_affected($stmt);


    if($updated_rows === false) {
        die(print_r(sqlsrv_errors(), true));
    } elseif($updated_rows == -1) {
        print "No Info available<br>";
    } else {
        print "Updated: $updated_rows rows<br>";
    }