PDO::beginTransaction
PDO::beginTransaction initiates a transaction (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
bool PDO::beginTransaction ( void )
Disables autocommit mode. While autocommit mode is disabled, changes made to the database via the PDO instance are not committed until you call PDO::commit() to end the transaction.
Calling PDO::rollBack() will revert the changes made to the database and return the database connection to autocommit mode.
Some databases, including MySQL, automatically issue an implicit commit when a DDL statement such as DROP TABLE or CREATE TABLE is issued.
The implicit commit prevents you from rolling back any other changes within the transaction scope.
Return Value
Examples
Rolling Back a Transaction
The following example starts a transaction and issues two database modification statements before rolling back the changes.
However, in MySQL, the DROP TABLE statement automatically commits the transaction, preventing any changes within this transaction from being rolled back.
<?php
/* Begin a transaction, turning off autocommit */
$dbh->beginTransaction();
/* Modify database schema and data */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
SET name = 'hamburger'");
/* Detect error and roll back changes */
$dbh->rollBack();
/* Database connection is now back to autocommit mode */
?>