Home  >  Article  >  Backend Development  >  Detailed explanation of the code case of transaction rollback implemented by php PDO

Detailed explanation of the code case of transaction rollback implemented by php PDO

黄舟
黄舟Original
2017-03-24 09:42:261032browse

This article mainly introduces the transaction rollback function implemented by php PDO, and analyzes the relevant SQL statements and related SQL statements of PDO-based transaction rollback function based on PDO operation based on specific examples. For operational skills, friends in need can refer to

. This article describes the transaction rollback implemented by php PDO. Share it with everyone for your reference, the details are as follows:

$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."<br>".$e->getMessage();
}
$conn=NULL;

The above is the detailed content of Detailed explanation of the code case of transaction rollback implemented by php PDO. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn