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