MySQL Transactions

MySQL has the ability to handle what are known as transactions: a series of queries that must be executed in an all-or-none fashion. As a bonus, it’s super easy to use.

Using PHP, you can do the following:

mysql_query("START TRANSACTION");
mysql_query("CREATE TABLE table");
mysql_query("CREATE TABLE table");
mysql_query("COMMIT");

When MySQL receives the START TRANSACTION query, it won’t execute anything else until it receives a COMMIT query. Upon receiving the COMMIT query, everything in the transaction is executed, and if anything fails, it all fails.

About Scott Allen

Scott Allen is a web developer from Indianapolis, Indiana. He has been working with HTML, CSS, PHP, & MySQL since the late '90s, and has extensive experience in database design and development, server-side scripting, content management, and front-end user experience, especially in the creation of educational content. Connect with Scott on Twitter (preferably), or Facebook or Google+ (if you aren't in a hurry).
This entry was posted in Code, Tech and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *