如何检查 UPDATE mysqli 查询是否正确执行?

2021-12-25 00:00:00 sql-update php mysqli prepared-statement

这是我的情况:

$sql = 'UPDATE user SET password = ? WHERE username = ? AND password = ?';
if($stmt->prepare($sql)) {
    $stmt->bind_param('sss', $newPass, $_SESSION['username'], $oldPass);
    $stmt->execute();
}

现在,如何查看 UPDATE 查询是否成功执行?更准确地说,我如何查看旧密码和用户名是否正确,以便我可以存储新密码?我试过这样做:

Now, how can I see if the UPDATE query is successfully executed? And more precisely how can I see if the old password and username are correct so that I can store the new password? I've tried by doing this:

$res = $stmt->execute();
echo 'Result: '.$res;

但我总是得到:

Result: 1

即使旧密码不正确.

推荐答案

不更新行的查询不是错误条件.这只是一个没有改变任何东西的成功查询.要查看更新是否确实改变了任何内容,您必须使用 mysqli_affected_rows()

A query which updates no rows is NOT an error condition. It's simply a succesful query that didn't change anything. To see if an update actually did change anything, you have to use mysqli_affected_rows()

相关文章