This article mainly introduces the transaction rollback function implemented by phpPDO, and analyzes the SQL statements and operation skills related to the function of implementing transaction rollback by php based on the PDO operation in combination with the specific instance form, for more information about the transaction rollback function implemented by php PDO, the SQL statements and operation skills related to implementing the transaction rollback function of php based on PDO operations are analyzed based on the specific instance form. For more information, see
This example describes the transaction rollback implemented by php PDO. We will share this with you 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 ); // start the transaction $ conn-> beginTransaction (); $ conn-> exec ("insert into 'hello' ('firstname', 'lastname', 'Email ') VALUES ('Ye ', 'xiaming', 'yexianming @ 163.com') "); $ conn-> exec (" insert into 'hello' ('firs Tname', 'lastname', 'Email ') VALUES ('Ye', 'cong', 'yecong @ 163.com ')"); $ conn-> exec ("insert into 'hello' ('firstname', 'lastname', 'Email ') VALUES ('fang', 'meng ', 'fangmeng @ 168.com ') "); // submit the transaction $ conn-> commit (); echo" New records created successfully! ";} Catch (PDOException $ e) {// roll back the transaction $ conn-> rollBack (); echo $ SQL ."
". $ E-> getMessage () ;}$ conn = NULL;
The above is a detailed description of the code case of transaction rollback implemented by php PDO. For more information, see other related articles in the first PHP community!