0

How to determine if Mysql update succeeded with PDO PHP?

Check affected rows

$sth->rowCount();

This would only work if the updated row has changes, but what if the row is unchanged?? Then $sth->rowCount() would return 0 even if the update has succeeded...

clarkk
  • 27,151
  • 72
  • 200
  • 340

1 Answers1

-1

The normal way to do this would be to construct a COUNT query on the same criteria, and run it before your UPDATE.

This could be subject to race conditions depending whether you're running in a transaction, and if so on the isolation level. So the real answer depends what you need the number for.

George Lund
  • 1,269
  • 12
  • 16
  • this should be part of comment instead of answer –  Aug 08 '13 at 19:27
  • This is actually *abnormal* way. Mind race conditions. – Your Common Sense Aug 08 '13 at 19:29
  • Nonsense, the OP wants to know how many rows matched his UPDATE, and isolating a COUNT in the same transaction is, to the best of my knowledge, the *only* way to achieve that. I'll look forward to seeing anyone else come up with a better answer. – George Lund Aug 08 '13 at 19:37