Php + Mysqli how to use transactions to handle transfers _ PHP Tutorial

Source: Internet
Author: User
Php + Mysqli use the transaction to handle the transfer problem. Php + Mysqli methods of transaction transfer handling this article describes how php + Mysqli uses transactions to handle transfer problems. Share with php + Mysqli how to use transactions to handle transfers

Php + Mysqli how to use transactions to handle transfers

This example describes how php + Mysqli processes the transfer problem using transactions. Share with you for your reference

?

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

36

37

38

Header ("Content-type: text/html; charset = utf-8 ");

$ Mysqli = new mysqli ("localhost", "root", "064319", "php ");

$ Mysqli-> set_charset ("utf8 ");

If ($ mysqli-> connect_errno ){

Die ('database connection failed'. $ mysqli-> connect_error );

}

$ Mysqli-> autocommit (false); // set the automatic submission mode to false.

$ Flag = true; // indicates whether the transaction is successfully executed.

$ Query = "update account set balance = balance-1000 where id = 3 ";

$ Result = $ mysqli-> query ($ query );

$ Affected_count = $ mysqli-> affected_rows;

If (! Result | $ affected_count = 0) {// failed

$ Flag = false;

}

$ Query = "update account set balance = balance + 1000 where id = 2 ";

$ Result = $ mysqli-> query ($ query );

$ Affected_count = $ mysqli-> affected_rows;

If (! $ Result | $ affected_count = 0 ){

$ Flag = false;

}

If ($ flag ){

$ Mysqli-> commit ();

Echo 'transfer successful ';

} Else {

$ Mysqli-> rollback ();

Echo 'transfer failed ';

}

$ Mysqli-> autocommit (true); // reset the transaction to automatic commit

$ Mysqli-> close ();

?>

Code 2:

?

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

36

37

38

39

40

41

42

43

Try {

$ Pdo = new PDO ("mysql: host = localhost; dbname = psp", "root ","");

$ Pdo-> exec ("set names utf8 ");

$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // sets the exception handling mode.

$ Pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 0); // disable automatic submission

} Catch (PDOException $ e ){

Echo "database connection failed ";

Exit;

}

Try {

$ Age = 10;

$ Pdo-> beginTransaction (); // start the transaction

$ Affected_rows1 = $ pdo-> exec ("update kfry set k_age = k_age + {$ age} where k_name = 'user1 '");

$ Affected_rows2 = $ pdo-> exec ("update kfry set k_age = k_age-{$ age} where k_name = 'user2'"); // modify it to make it successful or failed at will

/* If ($ affected_rows1 & $ affected_rows2)

{

$ Pdo-> commit ();

Echo "operation successful ";

} Else {

$ Pdo-> rollback ();

}*/

If (! $ Affected_rows1)

Throw new PDOException ("add error ");

If (! $ Affected_rows2)

Throw new PDOException ("reduce errors ");

Echo "operation successful ";

$ Pdo-> commit (); // if the first two updated SQL statements are successfully executed, the entire transaction is successfully executed.

} Catch (PDOException $ e ){

Echo "operation failed:". $ e-> getMessage ();

$ Pdo-> rollback (); // An error occurred while executing the statement in the transaction. The entire transaction is canceled.

}

$ Pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 1 );

// Test whether the task is successful

Echo "\ n operation result: \ n ";

$ SQL = "select * from kfry ";

$ Result = $ pdo-> query ($ SQL );

Foreach ($ result as $ v)

{

Echo $ v ['K _ name']. ". $ v ['K _ age']." \ n ";

}

?>

The above is all the content of this article. I hope you will like it.

Using php + Mysqli how to use transactions to handle transfers this article describes how php + Mysqli uses transactions to handle transfers. Share...

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.