How to check if mysqli UPDATE query execution is correct?

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 check if the UPDATE query succeeds? And more precisely, how can I check if the old password and username are correct so that I can save the new password? I tried to do this:

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

But I always get:

 Result: 1 

even if the old password is incorrect.

+7
source share
4 answers

A query that does not update rows is NOT an error condition. This is just a successful request that hasn’t changed anything. To make sure the update has really changed something, you should use mysqli_affected_rows ()

+13
source

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

+2
source

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

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

Try it.

0
source

All Articles