Example of a php + mysql transaction rollback & amp; commit

Source: Internet
Author: User

Mysql_query ("BEGIN"); // starts a transaction.
Mysql_query ("set autocommit = 0"); // you can specify that the transaction is not automatically committed.
$ Insert = "insert into userinfo VALUES ('aa12', 'A', '1', 'aaa ')";
Mysql_query ($ insert );
Mysql_query ("COMMIT"); // in non-autocommit mode, you must manually execute COMMIT to make the operation take effect.
// Mysql_query ("set autocommit = 0 ");
$ Insert = "insert into userinfo VALUES ('aa20', 'AA', '1', 'aaa ')";
$ Insert = "insert into userinfo VALUES ('aa8', 'A', '1 ')";
// Mysql_query ("COMMIT ");
If (mysql_num_rows = 0)
Mysql_query ("ROLLBACK"); // in non-autocommit mode, ROLLBACK is executed to invalidate the transaction operation.
Else echo "OK ";
// Mysql_query ("CLOSE ");
Mysql_query ("set autocommit = 1"); // restore autocommit Mode
$ Insert = "insert into userinfo VALUES ('aa15', 'A', '1', 'aaa ')";
Mysql_query ($ insert); // you do not need to manually execute COMMIT to make the transaction take effect.

After the BEGIN command is executed, set autocommit = 0, and set autocommit = 0 or 1 is invalid. Therefore, in order to make the operation clear, do not use BEGIN.

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.