//1、创建数据库连接对象
$mysqli = new MySQLi("localhost","root","123456","liuyan");
if($mysqli->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 "转账成功";
}
?>