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(). Throws a PDOException if no active transaction is present.
If the database is set to auto-commit mode, this function will restore auto-commit mode after rolling back the transaction.
Some databases, including MySQL, automatically commit the transaction when a DDL statement such as DROP TABLE or CREATE TABLE is issued within a transaction. This implicit commit prevents any further rollback of changes within this transaction scope.
Return Value
Returns TRUE on success or FALSE on failure.
Examples
Rolling back a transaction
The following example starts a transaction and issues two database-modifying statements before rolling back the changes. However, in MySQL, the DROP TABLE statement automatically commits the transaction, so no changes within this transaction are rolled back.
<?php
/* Begin a transaction, turning off auto-commit */
$dbh->beginTransaction();
/* Modify database schema and data */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
SET name = 'hamburger'");
/* Detect an error and roll back changes */
$dbh->rollBack();
/* Database connection returns to auto-commit mode */
?>