PHP MySQL Transaction processing

Source: Internet
Author: User
Tags php mysql vars

Transactions are required to meet 4 conditions (ACID): atomicity (autmic), consistency (consistency), isolation (isolation), persistence (durability)

Atomicity (autmic): The transaction is in execution, to do "either do it or do it all!" ", which means that the transactional part is not allowed to execute. Even if the transaction can not be completed because of the failure, also eliminate the impact on the database when rollback!

Consistency (consistency): Transactional operations should allow the database to transition from one consistent state to another consistent state! Take online shopping, you only have to let the goods out of the library, and let the goods into the customer's shopping basket to constitute a business!

Isolation (Isolation): If more than one transaction executes concurrently, it should be done independently of each transaction!

Persistence (Durability): A successful execution of a transaction has a lasting effect on the database and should be recoverable even if the database should fail in error!

There are two main ways to handle MySQL transactions.
1, with Begin,rollback,commit to achieve
Begin a transaction
ROLLBACK TRANSACTION Rollback
Commit TRANSACTION Acknowledgement
Rollback and commit cannot be used side by side when you use both, only the previous one is valid, and the latter is invalid, that is, you or perform commit or execute rollback
2, directly with set to change the MySQL automatic submission mode
MySQL is automatically submitted by default, that is, you submit a query, it is executed directly! We can pass
Set autocommit=0 prohibit auto-commit
Set autocommit=1 turn on auto-commit
To implement transaction processing.

Note that when you use set autocommit=0, all of your later SQL will be transacted until you end it with commit confirmation or rollback, and notice that when you end the transaction, you also open a new transaction! Press the first method to only present the current as a transaction!

Only InnoDB and BDB types of data tables in MySQL can support transactional processing! Other types are not supported!
: General MySQL Database The default engine is MyISAM, this engine does not support transactions! If you want to allow MySQL to support transactions, you can manually modify them:
The method is as follows: 1. Modify the C:\appserv\mysql\my.ini file, find the Skip-innodb, add # to the front, save the file.
2. In the operation, enter: services.msc, restart the MySQL service.
3. To phpMyAdmin, mysql->show engines; (or execute mysql->show variables like ' have_% '; ), viewing InnoDB as yes means that the database supports InnoDB.
It also indicates that the support transaction is transaction.
4. When creating a table, you can select the InnoDB engine for storage. If it is a previously created table, you can use Mysql->alter table table_name Type=innodb;
or Mysql->alter table table_name Engine=innodb; Change the engine of the data table to support transactions.

Example One.

PHP code
  1. $conn = mysql_connect (' localhost ',' root ',' root ') or Die ("Data connection Error!!!");
  2. mysql_select_db (' test ',$conn);
  3. mysql_query ("Set names ' GBK '");   //Use GBK Chinese code;
  4. Start a transaction
  5. mysql_query ("BEGIN");   //or mysql_query ("START TRANSACTION");
  6. $sql = "INSERT into ' user ' (' id ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')";
  7. $sql 2 = "INSERT into ' user ' (' Do ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')"; I wrote it wrong on purpose .
  8. $res = mysql_query ($sql);
  9. $res 1 = mysql_query ($sql 2);
  10. if ($res && $res 1) {
  11. mysql_query ("COMMIT");
  12. Echo ' submitted successfully.   ‘;
  13. }else{
  14. mysql_query ("ROLLBACK");
  15. echo ' data rollback.   ‘;
  16. }
  17. mysql_query ("END");

Example Two

PHP code
  1. mysql_query ("SET autocommit=0"); //Set MySQL not to submit automatically, you need to commit the commit statement by yourself
  2. $sql = "INSERT into ' user ' (' id ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')";
  3. $sql 2 = "INSERT into ' user ' (' Do ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')"; I wrote it wrong on purpose .
  4. $res = mysql_query ($sql);
  5. $res 1 = mysql_query ($sql 2);
  6. if ($res && $res 1) {
  7. mysql_query ("COMMIT");
  8. Echo ' submitted successfully.   ‘;
  9. }else{
  10. mysql_query ("ROLLBACK");
  11. echo ' data rollback.   ‘;
  12. }
  13. mysql_query ("END"); //Do not forget mysql_query ("SET autocommit=1") when the transaction is finished; autocommit

For MyISAM engine databases that do not support transactions, you can use table locking methods

MyISAM & InnoDB all support,
/*
Lock tables can lock a table for the current thread. If the table is locked by another thread, it will clog until all locks can be obtained.
UNLOCK tables can release any locks held by the current thread. When a thread publishes another lock tables, or when the connection to the server is closed, all tables locked by the current thread are implicitly unlocked.
*/

PHP code
    1. mysql_query ("LOCK TABLES ' user ' WRITE"); Lock the ' user ' table
    2. $sql = "INSERT into ' user ' (' id ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')";
    3. $res = mysql_query ($sql);
    4. if ($res) {
    5. Echo ' submitted successfully.! ';
    6. }else{
    7. echo ' failure! ';
    8. }
    9. mysql_query ("UNLOCK TABLES"); Release lock

PHP MySQL Transaction processing

Related Article

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.