Skip to content
Home » MySQL » MySQL Update: 0 rows affected

MySQL Update: 0 rows affected

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 = 'Foo Bar'";
$arr = array(':introduction' => "About Foo Bar");
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. Required fields are marked *