Home>Article>Backend Development> How to implement inter-bank transfer in php
How to implement inter-bank transfer in php: 1. Create a database connection object; 2. Set autocommit to false; 3. Implement bank transfer through php mysqli transaction control.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php How to realize inter-bank transfer?
php mysqli transaction control to implement bank transfer example
This article mainly introduces php mysqli transaction control to implement bank transfer. The example analyzes the principle of transaction control and the use of transaction rollback. For tips, friends who need it can refer to
Transaction Control, which means that all statements will not be submitted until they are executed successfully. Otherwise, if a previous statement was executed successfully but the subsequent statement was not executed successfully, it will be rolled back to the state before execution. This application is illustrated through the case of bank transfer. When money is transferred out from one account, money must be transferred into the other account for it to be considered successful.
The code is as follows:
connect_error){ die($mysqli->connect_error); } $mysqli->query("set names 'GBK'"); $mysqli->autocommit(false); //首先设置autocommit为false,也就是不自动提交 $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 "转账失败"; $mysqli->rollback();//如果有一条不成功,则回滚 }else{ $mysqli->commit();//两条语句都执行成功,则提交 echo "转账成功"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement inter-bank transfer in php. For more information, please follow other related articles on the PHP Chinese website!