Transaction processing is widely used in various management systems, such as personnel management systems. Many synchronization database operations mostly require transaction processing. For example, in the personnel management system, if you delete a person, you need to delete not only the basic information of the person, but also the information related to the person, such as mailbox, articles, etc. In this way, these databases The operation statement constitutes a transaction!
This article mainly introduces the transaction rollback function implemented by php PDO, and analyzes the relevant SQL statements and operation skills of php based on PDO operation to implement the transaction rollback function based on specific examples. Friends in need can refer to the following ,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 Transaction rollback sample code implemented by php PDO. For more information, please follow other related articles on the PHP Chinese website!