How to implement transaction rollback in php

墨辰丷
Release: 2023-03-27 19:28:01
Original
4938 people have browsed it

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 this article

The example describes the simple implementation method of PHP transaction rollback. 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;
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study .


Related recommendations:

ThinkphpTransaction Detailed explanation of operation examples

php based on pdoTransactionProcessing method

ThinkPHP 3.2.2TransactionOperation method

The above is the detailed content of How to implement transaction rollback in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!