//PDO事务
$pdn = "mysql:host=localhost;dbname=test";
$pdo = new PDO($pdn,"root","");//连接数据库
$pdo->exec('set names utf8');//设置字符集
$pdo->beginTransaction();//开启事务
$sql1 = "UPDATE users SET money=money-100 WHERE id=5";//修改数据SQL语句
$sql2 = "UPDATE users SET money=money+100 WHERE id=99";
$r1 = $pdo->exec($sql1);//执行SQL语句 更新数据库
$r2 = $pdo->exec($sql2);
if($r1>0&&$r2>0){
$pdo->commit();
echo '操作成功';
}else{
$pdo->rollback();
echo '事务回滚';
}
//$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
$sql3 = "INSERT INTO users(name,money) VALUES('test',12345)";
$r3 = $pdo->exec($sql3);
var_dump($r3);
测试了一下 不设置 $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1); 也能自动提交