UPDATE mysql queries... which must executed both or none

PDO usage
I have two UPDATE mysql queries… which must executed both or none … what can I use for this?

I think you’re looking for transactions, maybe.

http://php.net/manual/en/pdo.transactions.php

Warning
PDO only checks for transaction capabilities on driver level. If certain runtime conditions mean that transactions are unavailable, PDO::beginTransaction() will still return TRUE without error if the database server accepts the request to start a transaction.
An example of this would be trying to use transactions on MyISAM tables on a MySQL database.

2 Likes

You need a transaction here.

Basically you need to wrap your updates into try..catch and make sure that exception mode is enabled.
I’ve got a section dedicated to transactions in my PDO tutorial with an example little checklist of gotchas:

  • you have catch an Exception, not PDOException, as it doesn’t matter what particular exception aborted the execution.
  • you should re-throw an exception after rollback, to be notified of the problem the usual way.
  • also make sure that a table engine supports transactions (i.e. for Mysql it should be InnoDB, not MyISAM).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.