PHP uses MySQL transaction instance to parse

Source: Internet
Author: User
Tags commit rollback

The specific examples are as follows:

01 <?php
02 Database connection
03 $conn = mysql_connect (' localhost ', ' root ', ');
04 mysql_select_db (' Test ', $conn);
05 mysql_query ("SET NAMES GBK");
06
07 /*
08 The table that supports the transaction must be a InnoDB type
09 Only one occurrence of a transaction can occur:
10 mysql_query (' Start TRANSACTION ');/start transaction
11 mysql_query (' ROLLBACK ');/ROLLBACK TRANSACTION
12 mysql_query (' commit ');/COMMIT Transaction
13
14 If multiple rollback transactions occur in a transaction, when a transaction is committed, all operations on the database are canceled only before the first rollback to the start of the transaction, and all the database operations are still valid until the transaction is committed for the first rollback, so the rollback statement is typically placed only before the transaction statement is committed
15 If there is no commit statement for a transaction, all of the following database operations are performed from the start of the transaction (the execution method returns a right error) but have no effect on the database, but the previous segment is automatically committed when the next start transaction statement is executed
16 */
17 mysql_query (' START TRANSACTION ');
18 $isBad = 0;
19
20 $ins _testtable1 = "INSERT into Testtable1 (name,age) VALUES (' 23");
21st if (!mysql_query ($ins _testtable1)) {
22 $isBad = 1;
23 }
24 Error inserting statement field name
25 $ins _testtable2 = "INSERT into Testtable1 (name,ages) VALUES (' second ', ' 24 ')";
26 if (!mysql_query ($ins _testtable2)) {
27 $isBad = 1;
28 }
29 if ($isBad = = 1) {
30 Echo $isBad;
31 mysql_query (' ROLLBACK ');
32 }
33 mysql_query (' COMMIT ');
34 Mysql_close ($conn);
35 ?>
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.