$mysqli=@new mysqli(‘localhost‘,‘root‘,‘‘,‘sqldb‘);
if(mysqli_connect_errno()){
echo "链接错误".$mysqli->error.":".$mysqli->errno;
}
//关闭自动提交
$mysqli->autocommit(0);
$error=true;
$zhuan=50;
//张三
$sql="update user set total=total-{$zhuan} where name=‘zhangsan‘";
$result=$mysqli->query($sql);
if(!$result){
$error=false;
echo "张三转账失败";
}else{
if($mysqli->affected_rows==0){
$error=false;
echo "张三的钱没有变化";
}else{
$error=true;
echo "张三转账成功";
}
}
//李四
$sql="update user set total=total+{$zhuan} where name=‘lisi‘";
$result=$mysqli->query($sql);
if(!$result){
$error=false;
echo "李四转入失败";
}else{
if($mysqli->affected_rows==0){
$error=false;
echo "李四的钱没有变化";
}else{
$error=true;
echo "李四转入成功";
}
}
if($error){
echo "转账成功";
$mysqli->commit();
}else{
echo "转账失败";
$mysqli->rollback();
}
$mysqli->autocommit(1);