How to check if an UPDATE mysqli query is correctly executed?

家住魔仙堡 提交于 2019-12-29 01:35:52

问题


Here is my case:

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

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;

But I always get:

Result: 1

even if the old password is not correct.


回答1:


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()




回答2:


You need to use the affected_rows function of the MySQL extension you are using. This will return 0 if the query failed because no rows matched, -1 if an error occurred, or a positive number showing the number of rows that were changed.




回答3:


Try use mysqli_affected_rows() to get the number of affected rows.




回答4:


 mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50");
 printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link));

Try that.



来源:https://stackoverflow.com/questions/12990164/how-to-check-if-an-update-mysqli-query-is-correctly-executed

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