Example of transaction usage in PHP _php tutorial

Source: Internet
Author: User

Instance of transaction usage in PHP


This article mainly introduces the use of transaction examples in PHP, this article gives the simplest entry-level examples, the need for friends can refer to the following

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Database connection

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

mysql_select_db (' Test ', $conn);

mysql_query ("SET NAMES GBK");

/*

The table that supports transactions must be of type InnoDB

Can only occur once in a transaction:

mysql_query (' Start TRANSACTION ');//Start transaction

mysql_query (' ROLLBACK ');//ROLLBACK TRANSACTION

mysql_query (' commit ');//COMMIT Transaction

If the transaction is rolled back multiple times in a transaction, when the transaction is committed, all operations on the database are canceled after the first rollback until the start of the transaction, and after the first rollback until the transaction is committed, all the database operations are still valid, so the rollback statement is typically placed only before committing the transaction statement

If there is no commit statement for a transaction, all of the following database operations are executed from the start of the transaction (the execution method returns the wrong error), but there is no impact on the database, but the previous transaction is automatically committed when the next segment starts the transaction statement

*/

mysql_query (' START TRANSACTION ');

$isBad = 0;

$ins _testtable1 = "INSERT into Testtable1 (name,age) VALUES (' first ', 23)";

if (!mysql_query ($ins _testtable1)) {

$isBad = 1;

}

Error inserting 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);

?>

http://www.bkjia.com/PHPjc/1006580.html www.bkjia.com true http://www.bkjia.com/PHPjc/1006580.html techarticle example of transaction usage in PHP This article mainly introduces the use of transactions in PHP, the simplest example of entry-level examples, the need for friends to refer to the next? 1 2 3 4 5 6 7 8 9 ...

  • 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.