pdo事务
日期:2020-11-08 09:27:05 浏览:215
<?php
//事务
try {
$dsn="mysql:dbname=demo;host=localhost;port=3308";
$dbh=new PDO($dsn,'root','root');
}catch (PDOException $e){
echo $e->getMessage();
exit();
}
//关闭自动提交
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
//开启事务
echo $dbh->getAttribute(PDO::ATTR_AUTOCOMMIT);
$dbh->beginTransaction();
//查看是否开启自动提交
$result1=$dbh->exec('update jinrong set money=money-100 where name="张大美"');
$result2=$dbh->exec('update jinrong set money=money-200 where name="李阳"');
$result3=$dbh->exec('update jinrong set money=money+300 where name="罗晓玲"');
//
//
if($result1 && $result2 & $result3){
$dbh->commit();//提交
echo "更新成功";
}else{
$dbh->rollBack();//回滚事务
echo "更新失败";
}
//恢复自动提交
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
上一篇: cookie的应用
下一篇: php头部信息header自动刷新,下载