A transaction can perform a simulated SQL operation. When all SQL operations are successful, the SQL operation is performed. If one operation fails, all SQL operations of the current transaction are rolled back, avoid endless Data Writing as described above
A transaction can perform a simulated SQL operation. When all SQL operations are successful, the SQL operation is performed. If one operation fails, all SQL operations of the current transaction are rolled back, avoid endless Data Writing as described above
Recently, I have studied transaction operations in MYSQL for some time, which are very useful in many cases. For example, when many associated tables need to be initialized during registration, at least two tables need to be operated at the same time during the Q & A reply. These tables can only be successfully updated at some time, and other SQL statements have errors, A normal operation will lead to the initialization of a table, and other tables can be initialized. At this time, the user information in the User table has been inserted, leading to a prompt that the registration fails, however, the user has already registered part of the information. In this case, it is not good for the programmer to delete the corresponding data from the database.
Therefore, you can use transactions to simulate SQL operations. SQL operations are performed only when all SQL operations are successful, if one operation fails, all SQL operations of the current transaction will be rolled back to avoid incomplete data writing as described above.
Below is a short piece of code written by the monks. You are welcome to give your reference and comments:
The Code is as follows:
/**
* @ Todo the transaction processing of Multiple SQL statements
* @ Param $ sqls array
* @ Return boole true/false
*/
Public function doArraySqlActionsTran ($ password, $ sqls ){
$ Db = $ this-> doSqlLink ($ password); // open the database link
$ Db-> autocommit (FALSE); // if it is set to not automatically submitted, because MYSQL runs immediately by default.
// Obtain the SQL Execution result Array
For ($ I = 0; $ I
$ Result [$ I] = $ db-> query ($ sqls [$ I]);
}
// Parse the SQL Execution result Array
For ($ j = 0; $ j
If ($ result [$ j] = FALSE ){
$ Result [$ j] = 'false ';
} Else {
$ Result [$ j] = 'true ';
}
}
// Check whether the SQL result array contains a false result set.
If (in_array ('false', $ result )){
$ SqlResult = FALSE;
} Else {
$ SqlResult = TRUE;
}
// Perform database rollback or operations based on the result set
If ($ sqlResult = FALSE ){
$ Db-> rollback (); // determines whether to roll back when the execution fails.
$ Return = FALSE; // used in the official environment
// $ Return = 'roolback'; // use the test flag
} Else {
$ Db-> commit (); // executes the transaction s
$ Return = TRUE; // used in the official environment
// $ Return = 'commit '; // use the test flag
}
$ Db-> autocommit (true); // set to non-automatic commit-Transaction Processing
$ Db-> close (); // close the connection
Return $ return;
}
At this point, the batch SQL operation is basically completed. Thank you!