Php + mysqli transaction control implements bank transfer instances and mysqli bank transfers. Php + mysqli transaction control implement bank transfer instance. mysqli bank transfer this article describes how php + mysqli transaction control implement bank transfer. Share it with you for your reference. Specifically, php + mysqli transaction control is used to implement bank transfer instances and mysqli bank transfers
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:
<? Php // 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 automatically submit $ 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 (); // if both statements are successfully executed, submit echo "transfer successful" ;}?>
I hope this article will help you with php programming.
Examples in this article describes how to implement bank transfers using php + mysqli transaction control. Share it with you for your reference. Details...