Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PDO::rollBack

PDO::rollBack

PHP PDO Reference ManualPHP PDO Reference Manual

PDO::rollBack - Roll 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 no transaction is active, a PDOException is thrown.

If the database is set to autocommit mode, this function (method) restores autocommit mode after rolling back the transaction.

Some databases, including MySQL, automatically cause an implicit commit when a DDL statement such as dropping or creating a table is issued within a transaction. An implicit commit prevents any changes within this transaction scope from being rolled back.


Return Value

Returns TRUE on success, or FALSE on failure.


Example

Roll back a transaction

The example below starts a transaction and issues two statements that modify the database before rolling back the change. However, in MySQL, the DROP TABLE statement auto-commits the transaction, so any changes within this transaction are not rolled back.



beginTransaction();



/* Change database schema and data */

$sth = $dbh->exec("DROP TABLE fruit");

$sth = $dbh->exec("UPDATE dessert

    SET name = 'hamburger'");



/* Identify errors and roll back changes */

$dbh->rollBack();



/* Database connection is restored to auto-commit mode */

?>

PHP PDO Reference ManualPHP PDO Reference Manual