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");
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
12 mysql_query (' commit ');/COMMIT Transaction
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;
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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.