PHP+MYSQLI transaction control implement Bank transfer Example _php tutorial

Source: Internet
Author: User

Php+mysqli transaction control implements Bank transfer example


This article mainly introduced the PHP+MYSQLI transaction control realizes the bank transfer, the example analyzes the thing control principle and the thing rollback the use skill, needs the friend to refer

This paper describes the method of PHP+MYSQLI transaction control to realize bank transfer. Share to everyone for your reference. The specific analysis is as follows:

Transaction control, which means that all statements are executed successfully before they are committed. Otherwise, if a previous statement execution succeeds and no subsequent execution succeeds, the rollback is to the state before execution. Use the bank transfer case to illustrate the application. One account turns the money out, and the other account has to have money to transfer it.

The code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

1. Create a database Connection object

$mysqli = new Mysqli ("localhost", "root", "123456", "Liuyan");

if ($mysqli->connect_error) {

Die ($mysqli->connect_error);

}

$mysqli->query ("Set names ' GBK '");

$mysqli->autocommit (FALSE);

First set Autocommit to False, that is, do not commit automatically

$sql 1 = "Update account set balance=balance-2 where id=1;";

$sql 2 = "Update account set balance=balance+2 where id=2;";

$res 1 = $mysqli->query ($sql 1) or Die ($mysqli->error);

$res 2 = $mysqli->query ($sql 2) or Die ($mysqli->error);

if (! $res 1 | |! $res 2) {

echo "Transfer failed";

$mysqli->rollback ();//If one is unsuccessful, roll back

}else{

$mysqli->commit ();//Two statements are executed successfully, the commit

echo "Transfer success";

}

?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/950762.html www.bkjia.com true http://www.bkjia.com/PHPjc/950762.html techarticle PHP+MYSQLI transaction Control implements Bank Transfer example this article mainly introduced the PHP+MYSQLI transaction control realizes the bank transfer, the example analyses the principle of the thing control and the use skill of the thing rollback.

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