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

PDO::beginTransaction

PDO::beginTransaction

PHP PDO Reference ManualPHP PDO Reference Manual

PDO::beginTransaction - Initiate a transaction (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)


Description

Syntax



bool PDO::beginTransaction ( void )

Turns off autocommit mode. While autocommit mode is off, changes made to the database through a PDO object instance are not committed until PDO::commit() is called to end the transaction.

Calling PDO::rollBack() rolls back the changes made to the database and returns the database connection to autocommit mode.

Some databases, including MySQL, automatically perform an implicit transaction commit when a DDL statement such as DROP TABLE or CREATE TABLE is issued.

An implicit commit prevents you from rolling back any other changes within this transaction scope.


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 rollback changes */

$dbh->rollBack();



/* Database connection now returns to auto-commit mode */

?>


PHP PDO Reference ManualPHP PDO Reference Manual