Execute MYSQL transactions in PHP to solve data writing problems.

Source: Internet
Author: User
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!

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.