Skip to content

No Affected Rows When Updating Table

  • by
The following updating via PDO may fail without any error message in some cases:
$dbh = new PDO(...);
$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();

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.