事务处理能保证所有的sql操作一次性完成或回滚,mysql默认的MyISAM表类型是不支持事务处理的,如果需要做事务处理,需要把表类型换成InnoDB
<?php $dsn=‘mysql:host=localhost;dbname=mssc‘; $user=‘root‘; $password=‘‘; $orderno=‘111‘; $orderno1=‘222‘; try { $dbh=new PDO($dsn,$user,$password); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $dbh->beginTransaction(); $sql="INSERT INTO onethink_order(order_no) VALUES(:orderno)"; $stmt=$dbh->prepare($sql); $stmt->bindParam(‘:orderno‘,$orderno); $stmt->execute(); $sql="INSERT INTO onethink_order(order_no) VALUES(:orderno1)"; $stmt=$dbh->prepare($sql); $stmt->bindParam(‘:orderno1‘,$orderno1); $stmt->execute(); $dbh->commit(); } catch (PDOException $e) { $dbh->rollBack(); echo ‘SQL Query:‘.$sql.‘</br>‘; echo ‘Connection failed:‘.$e->getMessage(); } ?>
如果由于某种原因导致第二条sql没有执行,那整个操作将回滚到之前状态
原文:http://www.cnblogs.com/hltswd/p/6241596.html