PDO::rollBack
PDO::rollBack — Rolls back a transaction (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
bool PDO::rollBack ( void )
Rolls back the current transaction initiated by PDO::beginTransaction(). If there is no active transaction, a PDOException will be thrown.
If the database is set to autocommit mode, this function (method) will restore autocommit mode after rolling back the transaction.
In some databases, including MySQL, DLL statements such as dropping or creating tables automatically cause an implicit commit when issued within a transaction. The implicit commit will make it impossible to roll back any changes within the scope of this transaction.
Return Values
Returns TRUE on success, or FALSE on failure.
Examples
Roll back a transaction
The following example starts a transaction and issues two statements that modify the database before rolling back the changes. However, in MySQL, the DROP TABLE statement automatically commits the transaction, so none of the changes in this transaction will be rolled back.
<?php
/* 开始一个事务,关闭自动提交 */
$dbh->beginTransaction();
/* 更改数据库架构和数据 */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
SET name = 'hamburger'");
/* 识别错误且回滚更改 */
$dbh->rollBack();
/* 此时数据库连接恢复到自动提交模式 */
?>
Other Extensions
PHP PDO Reference Manual