使用mysqli完成事务处理
时间:2011-03-26 来源:卖身葬小强
<?php
/*======================================事务处理======================================*/
//建表时要使用InnoDB(type=InnoDB)
$mysqli=new MySQLi("localhost","root","","xiaoqiangdb");
//$mysqli->set_charset("utf8"); //设置字符集
$mysqli->autocommit(0); //关闭自动提交
$error=true;
$price=50;
$sql="UPDATE zhanghaodb set yue=yue-{$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 zhanghaodb set yue=yue+{$price} where name='lisi'";
$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 "转账成功!";
}else{
echo "转账失败!";
$mysqli->rollback(); //回滚
}
$mysqli->autocommit(1); //开启自动提交
$mysqli->close();
?>
相关阅读 更多 +