I\'m doing an update query with PDO. I would like to figure out if my update query did not change anything in the database, since:
You could add conditionals to your 'where' clause so such as " and ColumnToUpdate <> 'NewValue'"
I've solved it using the suggestions of @hjpotter92.
// UID is the unique ID of my table, autoincremented etc...
// Firstly, let's try to update my row
$query = 'UPDATE my_table SET x=0, y=1, uid=LAST_INSERT_ID(uid) WHERE z=2';
$sth = $dbh->prepare($query);
if($sth->execute()) {
if($dbh->lastInsertId() == 0) { // Record was not found, so insert it.
$query = 'INSERT INTO my_table (x,y) VALUES (0,1)';
$sth = $dbh->prepare($query);
$sth->execute();
if($sth->rowCount() > 0) {
echo $dbh->lastInsertId(); // Return the UID of the inserted row
}
}
}