Home > Article > Backend Development > Detailed explanation of PDO::beginTransaction usage
This article mainly introduces you to the detailed usage of PDO::beginTransaction. I hope it will be helpful to friends in need!
PDO::beginTransaction(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
PDO::beginTransaction starts a transaction.
Syntax description:
PDO::beginTransaction ( void ) : bool
Turn off automatic submission mode. When autocommit mode is turned off, changes made to the database through PDO object instances are not committed until PDO::commit() is called to end the transaction. Calling PDO::rollBack() rolls back changes made to the database and returns the database connection to autocommit mode.
Some databases, including MySQL, will automatically commit an implicit transaction when issuing a DDL statement such as DROP TABLE or CREATE TABLE. Implicitly committing will prevent you from rolling back any other changes within the scope of this transaction.
Return value:
Returns TRUE on success, or FALSE on failure.
Code example:
Rolling back a transaction
The following example rolls back this change Before starting a transaction and issuing two statements that modify the database. But in MySQL, the DROP TABLE statement automatically commits the transaction so that any changes in this transaction will not be rolled back.
<?php /* 开始一个事务,关闭自动提交 */ $dbh->beginTransaction(); /* 更改数据库架构及数据 */ $sth = $dbh->exec("DROP TABLE fruit"); $sth = $dbh->exec("UPDATE dessert SET name = 'hamburger'"); /* 识别出错误并回滚更改 */ $dbh->rollBack(); /* 数据库连接现在返回到自动提交模式 */ ?>
Related recommendations: "PHP Tutorial"
The above is the detailed content of Detailed explanation of PDO::beginTransaction usage. For more information, please follow other related articles on the PHP Chinese website!