Home  >  Article  >  Backend Development  >  How to implement inter-bank transfer in php

How to implement inter-bank transfer in php

藏色散人
藏色散人Original
2021-11-16 10:20:102025browse

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.

How to implement inter-bank transfer in php

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:

<?php
//1、创建数据库连接对象
$mysqli = new MySQLi("localhost","root","123456","liuyan");
if($mysqli->connect_error){
 die($mysqli->connect_error);
}
$mysqli->query("set names &#39;GBK&#39;");
 
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn