• We generally use when working with transactions looks like this (semi-pseudo-code):
[pastacode lang=”sql” manual=”try%20%7B%0A%20%20%20%20%2F%2F%20First%20of%20all%2C%20let’s%20begin%20a%20transaction%0A%20%20%20%20%24db-%3EbeginTransaction()%3B%0A%0A%20%20%20%20%2F%2F%20A%20set%20of%20queries%3B%20if%20one%20fails%2C%20an%20exception%20should%20be%20thrown%0A%20%20%20%20%24db-%3Equery(‘first%20query’)%3B%0A%20%20%20%20%24db-%3Equery(‘second%20query’)%3B%0A%20%20%20%20%24db-%3Equery(‘third%20query’)%3B%0A%0A%20%20%20%20%2F%2F%20If%20we%20arrive%20here%2C%20it%20means%20that%20no%20exception%20was%20thrown%0A%20%20%20%20%2F%2F%20i.e.%20no%20query%20has%20failed%2C%20and%20we%20can%20commit%20the%20transaction%0A%20%20%20%20%24db-%3Ecommit()%3B%0A%7D%20catch%20(Exception%20%24e)%20%7B%0A%20%20%20%20%2F%2F%20An%20exception%20has%20been%20thrown%0A%20%20%20%20%2F%2F%20We%20must%20rollback%20the%20transaction%0A%20%20%20%20%24db-%3Erollback()%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Note that, with this idea, if a query fails, an Exception must be thrown:

PDO can do that, depending on how you configure it

  • See PDO::setAttribute
  • and PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION

else, with some other API, you might have to test the result of the function used to execute a query, and throw an exception yourself.

You cannot just put an instruction somewhere and have transactions done automatically: you still have to specific which group of queries must be executed in a transaction.

For example, quite often you’ll have a couple of queries before the transaction (before the begin) and another couple of queries after the transaction (after either commit or rollback) and you’ll want those queries executed no matter what happened (or not) in the transaction.

[pastacode lang=”sql” manual=”mysql_query(%22START%20TRANSACTION%22)%3B%0A%0A%24a1%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(‘1’)%22)%3B%0A%24a2%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(‘2’)%22)%3B%0A%0Aif%20(%24a1%20and%20%24a2)%20%7B%0A%20%20%20%20mysql_query(%22COMMIT%22)%3B%0A%7D%20else%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20mysql_query(%22ROLLBACK%22)%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • we made a function to get a vector of queries and do a transaction, maybe someone will find out it useful:
[pastacode lang=”sql” manual=”function%20transaction%20(%24con%2C%20%24Q)%7B%0A%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22START%20TRANSACTION%22)%3B%0A%0A%20%20%20%20%20%20%20%20for%20(%24i%20%3D%200%3B%20%24i%20%3C%20count%20(%24Q)%3B%20%24i%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!mysqli_query%20(%24con%2C%20%24Q%5B%24i%5D))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20echo%20’Error!%20Info%3A%20%3C’%20.%20mysqli_error%20(%24con)%20.%20’%3E%20Query%3A%20%3C’%20.%20%24Q%5B%24i%5D%20.%20’%3E’%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20if%20(%24i%20%3D%3D%20count%20(%24Q))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22COMMIT%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22ROLLBACK%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • One more procedural style example with mysqli_multi_query, assumes $query is filled with semicolon-separated statements.
[pastacode lang=”sql” manual=”mysqli_begin_transaction%20(%24link)%3B%0A%0Afor%20(mysqli_multi_query%20(%24link%2C%20%24query)%3B%0A%20%20%20%20mysqli_more_results%20(%24link)%3B%0A%20%20%20%20mysqli_next_result%20(%24link)%20)%3B%0A%0A!%20mysqli_errno%20(%24link)%20%3F%0A%20%20%20%20mysqli_commit%20(%24link)%20%3A%20mysqli_rollback%20(%24link)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • check which storage engine you are using. If it is MyISAM, then Transaction(‘COMMIT’,’ROLLBACK’) will not be supported because only the InnoDB storage engine, not MyISAM, supports transactions.

PHP MySQL transaction example

  • First, execute the following statement to create the accounts table:
[pastacode lang=”sql” manual=”CREATE%20TABLE%20accounts%20(%0A%20%20%20%20id%20%20%20%20%20INT%20AUTO_INCREMENT%20PRIMARY%20KEY%2C%0A%20%20%20%20name%20%20%20VARCHAR%20(50)%20%20%20%20NOT%20NULL%2C%0A%20%20%20%20amount%20DECIMAL%20(19%2C%204)%20NOT%20NULL%0A)%3B%0ASecond%2C%20insert%20two%20rows%20into%20the%20accounts%20table%3A%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

  • Second, insert two rows into the accounts table:
[pastacode lang=”sql” manual=”INSERT%20INTO%20accounts(name%2Camount)%0AVALUES(‘John’%2C25000)%2C%0A%20%20%20%20%20%20(‘Mary’%2C95000)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • Third, query the accounts table:

SELECT * FROM accounts;

Categorized in: