Example of transaction usage in PHP

Source: Internet
Author: User

Example of transaction usage in PHP

This article mainly introduces the example of transaction usage in PHP. This article provides the simplest entry-level instance. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<? Php

// Database connection

$ Conn = mysql_connect ('localhost', 'root ','');

Mysql_select_db ('test', $ conn );

Mysql_query ("set names gbk ");

 

/*

The tables that support transactions must be InnoDB

A transaction can only appear once:

Mysql_query ('start transaction'); // START the TRANSACTION

Mysql_query ('rollback'); // roll back the transaction

Mysql_query ('commit '); // submit a transaction

 

If multiple rollback transactions occur in a transaction, all database operations are canceled before the first rollback to the start of the transaction when the transaction is committed, after the first rollback, all database operations will still be valid until the transaction is committed. Therefore, the rollback statement is generally placed only before the transaction statement is committed.

If a transaction does not have a commit statement, all of the following operations on the database are performed from the start of the transaction (the execution method returns the right or wrong), but the database is not affected, however, when the next transaction statement is executed, the previous transaction is automatically committed.

*/

Mysql_query ('start transaction ');

$ IsBad = 0;

 

$ Ins_testTable1 = "insert into testtable1 (NAME, age) VALUES ('first', 23 )";

If (! Mysql_query ($ ins_testTable1 )){

$ IsBad = 1;

}

// The insert statement field name is incorrect.

$ 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 );

?>

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.