Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 475
Dim13i Avatar asked Oct 20 '12 15:10

Dim13i


2 Answers

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

like image 158
Marc B Avatar answered Oct 22 '22 21:10

Marc B


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

like image 41
Sven Avatar answered Oct 22 '22 19:10

Sven