Examples of detailed analysis of PHP+MYSQL transaction processing example

Source: Internet
Author: User

First, the data engine InnoDB with Begin,rollback,commit to commit the transaction, begin the transaction after the error occurs ROLLBACK TRANSACTION rollback or no error on commit transaction commit confirmation completed.
Start transaction begins with the statement block between the transaction begin and end, set autocommit=0 does not allow the transaction to be automatically committed after setting to end the set autocommit=1.
The following is an example of a data table engine that supports transactional processing.

mysql_query("Start Transaction");mysql_query("Set autocommit=0");mysql_query("Begin"); $sql= "INSERT into student (Name,num) VALUES (' test1 ', ' 0 ')";$sql 2= "INSERT into student (Name,num) VALUES (null, ' 0 ')";//Wrong Writing$res=mysql_query($sql);$res 1=mysql_query($sql 2); if($res&&$res 1){    mysql_query("Commit"); Echo' submitted successfully. <br/> ';}Else{    mysql_query("Rollback"); Echo' Data rollback. <br/> ';}mysql_query("Set Autocommit=1");mysql_query("End");

Second, the table locking method can be used for MyISAM engine databases that do not support transactions:
MyISAM & InnoDB All support, lock tables can lock the 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.
The following example is an example of using table locks for transactions that do not support commit

mysql_query("Lock tables Student write");//Lock Student Table write can also be read$sql= "INSERT into student (Name,num) VALUES (' Sansheng III ', ' 0 ')";$res=mysql_query($sql);if($res){    Echo' submitted successfully.!<br/> ';}Else{    Echo' Failure!<br/> ';}mysql_query("UNLOCK TABLES");//Release Lock

Complete example of the above code: HTTP://PAN.BAIDU.COM/S/1PLKBKKJ

Password: bh49

Examples of detailed analysis of PHP+MYSQL transaction processing example

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.