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. &nbsp; &nbsp; 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. &nbsp; &nbsp; Below is a small piece of code written below, welcome you to refer to and submit comments:&nbsp; code as follows:/**&nbsp; * @todo Multiple SQL transactions &nbsp; * @param $sqls array&nbsp; * @retur N Boole true/false&nbsp; */&nbsp; public Function Doarraysqlactionstran ($password, $sqls) {&nbsp; $db = $this-&gt; Dosqlli NK ($password);//Open Database link &nbsp; $db-&gt; autocommit (FALSE);//set to not autocommit because MySQL defaults to execute immediately &nbsp;//Get SQL Execution result array &nbsp; for ($i =0 $i &lt;count ($SQLS); $i + +) {&nbsp; $result [$i] = $db-&gt; query ($sqls [$i]);&nbsp;}&nbsp;//Parse SQL execution result array &nbsp; for ($j = 0; $j &lt;count ($result); $j + +) {&nbsp; if ($result [$j]==false) {&nbsp; $result [$j]= ' FALSE ';&nbsp;}else{&nbsp; $result [ $j]= ' true ';&nbsp;}&nbsp;}&nbsp;//Find false result set in SQL results array &nbsp; if (In_array (' FALSE ', $result)) {&nbsp; $sqlResult =false;&nbsp;}else{&nbsp; $sqlResult ==true;&nbsp;}&nbsp;//Database rollback based on result set or perform action &nbsp; if ($sqlResult ==false) {&nbsp; $db-&gt; rollback ()//To determine the use of &nbsp;//$return in the formal environment for rollback &nbsp; $return =false;//when execution fails ' Roolback ';//test tag using &nbsp;}else{&nbsp; $db-&gt; commit ()//Execution Transaction s&nbsp; $return =true;//formal environment using &nbsp;//$return = ' COMMIT ';//test tags use &nbsp;}&nbsp; $db-&gt;autocommit (true); Set to non-autocommit-transaction &nbsp; $DB-&gt;close ()//close connection &nbsp; return $return;&nbsp;}&nbsp; &nbsp; To perform bulk SQL operations on this transaction is basically done, thank you! &nbsp; &nbsp;
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019


Learn more >

Alibaba Cloud Free Trial


Learn more >

Contact Us

