<?php $mysqli=new mysqli("localhost", "root", "123456", "xsphpdb");
//关闭事务处理 $mysqli->autocommit(0);
$error=true;
$price=50;
$sql="update zh set ye=ye-{$price} where name=‘zhangsan‘"; $result=$mysqli->query($sql);
if(!$result){ $error=false; echo "从张三转出失败<br>"; }else{ if($mysqli->affected_rows==0){ $error=false; echo "张三的钱没有变化"; }else{ echo "从张三账号中转出成功!<br>"; } }
$sql="update zh set ye=ye+{$price} where name=‘lisi1‘";
$result=$mysqli->query($sql);
if(!$result){ $error=false; echo "从李四转入失败<br>"; }else{ if($mysqli->affected_rows==0){ $error=false; echo "李四的钱没有变化"; }else{ echo "向李四账号中转入成功!<br>"; } }
if($error){ echo "转账成功!"; $mysqli->commit(); }else{ echo "转账失败!"; $mysqli->rollback(); }
$mysqli->autocommit(1);//完成后开启事务处理 $mysqli->close();