This article mainly introduced the PHP transaction rollback simple implementation method, combined with the instance form analysis PHP transaction definition, the submission and the rollback transaction concrete operation skill, needs the friend to consult. We hope to help you.
Specific 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 transaction $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 ') "); Commit TRANSACTION $conn->commit (); echo "New Records created successfully!";} catch (Pdoexception $e) { //ROLLBACK TRANSACTION $conn->rollback (); echo $sql. " <br> ". $e->getmessage ();} $conn =null;
Related recommendations:
PHP database development Three points you need to know.
PHP's detailed PDO
Introduction to the functional operation of the PDO data Access abstraction layer in PHP