MySQL transaction in PHP to resolve data write incomplete and so on

Source: Internet
Author: User
Tags commit rollback

  transactions can simulate SQL operations, SQL operations when all SQL operations are successful, and all SQL operations of the current transaction are rolled back whenever an operation fails, avoiding the occurrence of the data written in the description above

Recently, a little time has been spent on the transaction operations in MySQL, in many cases, for example, when you need to initialize a lot of related tables when you register, you need to operate at least two tables at the same time, which in some cases will only successfully update a table, and another SQL statement error, A normal operation causes a table to be initialized. Other wood has the ability to initialize, this time will cause the user table user information has been inserted, causing the registration failed, but the user has registered some information, this time the programmer to the database to delete the corresponding data is a relatively bad thing.     Therefore this side consider the use of transactions, transactions can be simulated SQL operations, when all of the SQL operation is successful in the SQL operation, as long as there is a failure to rollback the current transaction of all SQL operations, to avoid the above described in the data write incomplete and so on.     Below is a small piece of code written below, welcome you to refer to and submit comments:  code as follows:/**  * @todo Multiple SQL transactions   * @param $sqls array  * @retur N Boole true/false  */  public Function Doarraysqlactionstran ($password, $sqls) {  $db = $this-> Dosqlli NK ($password);//Open Database link   $db-> autocommit (FALSE);//set to not autocommit because MySQL defaults to execute immediately  //Get SQL Execution result array   for ($i =0 $i <count ($SQLS); $i + +) {  $result [$i] = $db-> query ($sqls [$i]); } //Parse SQL execution result array   for ($j = 0; $j <count ($result); $j + +) {  if ($result [$j]==false) {  $result [$j]= ' FALSE '; }else{  $result [ $j]= ' true '; } } //Find false result set in SQL results array   if (In_array (' FALSE ', $result)) {  $sqlResult =false; }else{  $sqlResult ==true; } //Database rollback based on result set or perform action   if ($sqlResult ==false) {  $db-> rollback ()//To determine the use of  //$return in the formal environment for rollback   $return =false;//when execution fails ' Roolback ';//test tag using  }else{  $db-> commit ()//Execution Transaction s  $return =true;//formal environment using  //$return = ' COMMIT ';//test tags use  }  $db->autocommit (true); Set to non-autocommit-transaction   $DB->close ()//close connection   return $return; }    To perform bulk SQL operations on this transaction is basically done, thank you!    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.