MySQL

No Affected Rows When Updating Table

The following updating via PDO may fail without any error message in some cases:
<?php
...
$dbh = new PDO(...);
$dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = "update table_name set introduction = :introduction where name = 'Ed Chen'";
$arr = array(':introduction' => "Ed Chen's logic");
try {
$stmt = $dbh -> prepare($stmt);
$stmt -> execute($arr);
$count = $stmt -> rowCount();
echo "Affected rows: " . $count;
} catch (PDOException $e) {
echo $e -> getMessage();
die();
}
?>

The output is:
Affected rows: 0

It means no row was updated. I know it might cost you a lot of time to do a thorough check, but found nothing wrong. The fact is that MySQL does not update rows with the same values as the rows have, thus, no row is affected.

Leave a Reply

Your email address will not be published. Required fields are marked *