当前位置:编程学习 > php >>

php PDO实现事务回滚的方法

本文实例讲述了php PDO实现的事务回滚。分享给大家供大家参考,具体如下:
 
$servername="localhost"
$username="root";
$password="123456";
$dbname="zzzyk_com";

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('com','zzzyk','www@zzzyk.com')");
  $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('com','yizuotu','www@yizuotu.com')");
  $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('net','yizuotu','www@yizuotu.net')");

  //提交事务
  $conn->commit();
  echo "New records created successfully!";

}catch(PDOException $e){

   //回滚事务
   $conn->rollBack();
   echo $sql."<br>".$e->getMessage();

}
$conn=NULL;
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,