Use Mysql transaction instance parsing in PHP

Source: Internet
Author: User
Tags php database
This article mainly introduces the usage of Mysql transactions in PHP and is a very important practical operation skill in PHP database programming, for more information about how to use MySQL in PHP, see the following examples. Share it with you for your reference.

The specific example is as follows:

<? Php // database connection $ conn = mysql_connect ('localhost', 'root', ''); mysql_select_db ('test', $ conn ); mysql_query ("set names gbk");/* tables that support transactions must be InnoDB-type. a TRANSACTION can only appear once: mysql_query ('Start transaction '); // start the transaction mysql_query ('rollback'); // roll back the transaction mysql_query ('commit '); // submit the transaction. if multiple ROLLBACK transactions occur in a transaction, when a transaction is committed, all operations on the database are canceled before the first rollback and after the transaction starts. after the first rollback, all operations on the database remain valid until the transaction is committed, therefore, the rollback statement is generally placed only before the transaction statement is submitted. if a transaction does not have a commit statement, all the following operations on the database are executed at the start of the transaction (the execution method returns the right or wrong ), but it has no impact on the database, but in the lower part of the execution When starting a TRANSACTION statement, the previous TRANSACTION will automatically submit */mysql_query ('Start transaction'); $ isBad = 0; $ ins_testTable1 = "insert into testtable1 (NAME, age) VALUES ('first', 23) "; if (! Mysql_query ($ ins_testTable1) {$ isBad = 1;} // An error occurred while inserting the statement Field NAME $ ins_testTable2 = "insert into testtable1 (NAME, ages) VALUES ('second ', '24') "; if (! Mysql_query ($ ins_testTable2) {$ isBad = 1;} if ($ isBad = 1) {echo $ isBad; mysql_query ('rollback ');} mysql_query ('commit '); mysql_close ($ conn);?>

I hope the examples described in this article will help you learn the PHP + MySQL programming.

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.