MySQL Transactions in PHP

Sometimes it is critical to ensure a group of queries are all executed successfully to maintain the integrity of our data. Let's say we have a banking app where we need to subtract funds from one account and add funds to another:

copyraw
$mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'-1000000 WHERE 'user' = 'Bob'");
$mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'+1000000 WHERE 'user' = 'Fred'");
  1.  $mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'-1000000 WHERE 'user' = 'Bob'")
  2.  $mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'+1000000 WHERE 'user' = 'Fred'")

What if one of the queries fails? To mitigate the chances of misplacing our millions due to some possible error we must ensure that both queries are executed with the expected result before committing any changes to the data. This where transactions are useful. We can run the queries, then check if some conditions are met before committing the changes in the data:

copyraw
$mysqli->begin_transaction();
$mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'-1000000 WHERE 'user' = 'Bob'");
$a1=$mysqli->affected_rows();

$mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'+1000000 WHERE 'user' = 'Fred'");
$a2=$mysqli->affected_rows();

//Check 1 row affected by each query
if ($a1==1 && $a2==1) {
	
	//Everything looks OK, commit changes
	$mysqli->commit();
	
} else 

	//Something went wrong. Roll back to before begin_transaction()
	$mysqli->rollback();
}
$mysqli->close();
  1.  $mysqli->begin_transaction()
  2.  $mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'-1000000 WHERE 'user' = 'Bob'")
  3.  $a1=$mysqli->affected_rows()
  4.   
  5.  $mysqli->query ("UPDATE 'accounts' SET 'balance' = 'balance'+1000000 WHERE 'user' = 'Fred'")
  6.  $a2=$mysqli->affected_rows()
  7.   
  8.  //Check 1 row affected by each query 
  9.  if ($a1==1 && $a2==1) { 
  10.   
  11.      //Everything looks OK, commit changes 
  12.      $mysqli->commit()
  13.   
  14.  } else 
  15.   
  16.      //Something went wrong. Roll back to before begin_transaction() 
  17.      $mysqli->rollback()
  18.  } 
  19.  $mysqli->close()

mysqli->begin_transaction() requires PHP 5.6 and above. For lower versions you can issue the MySQL commands directly like:

mysqli->query("START TRANSACTION")
mysqli->query("COMMIT")
mysqli->query("ROLLBACK")

Important note: Our tables must use an engine that supports transactions ie. InnoDB. Don't be silly like me and spend hours wondering why your transactions aren't working on MyISAM tables.

Category: MySQL :: Article: 644

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.