Php + mysqli transaction control implementation of bank transfer instance _ PHP Tutorial

Source: Internet
Author: User
Php + mysqli transaction control for bank transfer instances. Php + mysqli transaction control implement bank transfer instance this article mainly introduces php + mysqli transaction control implement bank transfer, the example analyzes the principle of transaction control and the use of transaction rollback skills php + mysqli transaction control to implement bank transfer instances.

This article mainly introduces php + mysqli transaction control to implement bank transfers. the instance analyzes the principle of transaction control and the use of transaction rollback techniques. For more information, see

This article describes how to implement bank transfers using php + mysqli transaction control. Share it with you for your reference. The specific analysis is as follows:

Transaction control, that is, all statements are submitted after successful execution. Otherwise, if a previous statement is successfully executed but not later, it is rolled back to the status before execution. This application is illustrated through bank transfer cases. If one account transfers the money, the other account must transfer the money.

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

21

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 submit automatically

$ Sql1 = "update account set balance = balance-2 where id = 1 ;";

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

$ Res1 = $ mysqli-> query ($ sql1) or die ($ mysqli-> error );

$ Res2 = $ mysqli-> query ($ sql2) or die ($ mysqli-> error );

If (! $ Res1 |! $ Res2 ){

Echo "transfer failed ";

$ Mysqli-> rollback (); // if one fails, roll back

} Else {

$ Mysqli-> commit (); // submit if both statements are successfully executed

Echo "transfer successful ";

}

?>

I hope this article will help you with php programming.

This article introduces php + mysqli transaction control to implement bank transfers. the instance analyzes the principle of transaction control and the use of transaction rollback techniques...

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.