Home  >  Article  >  Backend Development  >  Transaction rollback example of PHP operation database

Transaction rollback example of PHP operation database

*文
*文Original
2018-01-02 17:17:041843browse

This article mainly introduces the simple implementation method of PHP transaction rollback, and analyzes the definition of PHP transactions, the specific operation skills of submitting and rolling back transactions in the form of examples. Friends in need can refer to it. I hope to be helpful.

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','1111@163.com')");
      $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','CONG','2222@163.com')");
      $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('FANG','MENG','3333@163.com')");
      //提交事务
      $conn->commit();
      echo "New records created successfully!";
}catch(PDOException $e){
       //回滚事务
       $conn->rollBack();
       echo $sql."<br>".$e->getMessage();
}
$conn=NULL;

Related recommendations:

##Three things you need to know about php database development

PHP Detailed Explanation of PDO

##Introduction to the functional operations of the PDO data access abstraction layer in PHP

The above is the detailed content of Transaction rollback example of PHP operation database. 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