本文实例讲述了php事务回滚简单实现方法。分享给大家供大家参考,具体如下:
$servername="localhost";
$username="root";
$password="admin";
$dbname="test";
try{
$conn=new pdo("mysql:host=$servername;dbname=$dbname",$username,$password);
$conn->setattribute(pdo::attr_errmode,pdo::errmode_exception);
//开始事务
$conn->begintransaction();
$conn->exec("insert into `hello`(`firstname`,`lastname`,`email`)values('ye','xiaming','yexianming@163.com')");
$conn->exec("insert into `hello`(`firstname`,`lastname`,`email`)values('ye','cong','yecong@163.com')");
$conn->exec("insert into `hello`(`firstname`,`lastname`,`email`)values('fang','meng','fangmeng@168.com')");
//提交事务
$conn->commit();
echo "new records created successfully!";
}catch(pdoexception $e){
//回滚事务
$conn->rollback();
echo $sql."
".$e->getmessage();
}
$conn=null;
希望本文所述对大家php程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!