Php+mysql transaction Rollback&commit Sample _php Tips

Source: Internet
Author: User
Tags rollback
mysql_query ("Begin");/Start a transaction
mysql_query ("SET autocommit=0"); Set transaction not automatic commit
$insert = "INSERT INTO UserInfo VALUES (' aa12 ', ' AA ', ' 1 ', ' aaa ')";
mysql_query ($insert);
mysql_query ("commit");//non-autocommit mode, you must manually perform a COMMIT to make the operation effective
mysql_query ("SET autocommit=0");
$insert = "INSERT INTO UserInfo VALUES (' aa20 ', ' AA ', ' 1 ', ' aaa ')";
$insert = "INSERT INTO UserInfo VALUES (' aa8 ', ' AA ', ' 1 ')";
mysql_query ("COMMIT");
if (mysql_num_rows==0)
mysql_query ("ROLLBACK")//non-autocommit mode, performing ROLLBACK to invalidate transaction operations
else echo "OK";
mysql_query ("close");
mysql_query ("SET autocommit=1");//Recovery autocommit mode
$insert = "INSERT INTO UserInfo VALUES (' aa15 ', ' AA ', ' 1 ', ' aaa ')";
mysql_query ($insert);//You do not need to perform a commit manually to make the transaction operational

After the begin is performed, its function is the same as set autocommit=0, and setting autocommit=0 or 1 o'clock is not valid thereafter. Therefore, in order to make the operation clear, generally do not use begin.
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.