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!