Php+mysqli transaction control implements bank transfer example, MYSQLI bank transfer
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:
<?PHP//1, CREATE 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 commit $SQL1 = "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, rollback}else{$mysqli->commit ();//Two statements executed successfully, commit echo "Successful transfer";}? >
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/949450.html www.bkjia.com true http://www.bkjia.com/PHPjc/949450.html techarticle PHP+MYSQLI transaction Control implements bank transfer example, MYSQLI Bank transfer This paper describes the method of PHP+MYSQLI transaction control to realize bank transfer. Share to everyone for your reference. Specific points ...