Documentation PHP

PDO::beginTransaction

(PHP 5 >= 5.1.0, PECL pdo:0.1-1.0.3)

PDO::beginTransaction — Initiates a transaction

Description

bool PDO::beginTransaction ( void )

Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO object instance are not committed until you end the transaction by calling PDO::commit(). Calling PDO::rollBack() will roll back all changes to the database and return the connection to autocommit mode.

Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit COMMIT will prevent you from rolling back any other changes within the transaction boundary.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Exemples

Exemple #1 Roll back a transaction

The following example begins a transaction and issues two statements that modify the database before rolling back the changes. On MySQL, however, the DROP TABLE statement automatically commits the transaction so that none of the changes in the transaction are rolled back.

<?php
/* Begin a transaction, turning off autocommit */
$dbh->beginTransaction();

/* Change the database schema and data */
$sth $dbh->exec("DROP TABLE fruit");
$sth $dbh->exec("UPDATE dessert
    SET name = 'hamburger'"
);

/* Recognize mistake and roll back changes */
$dbh->rollBack();

/* Database connection is now back in autocommit mode */
?>



Ceci n'est pas la documentation originale du langage de programmation php, pour y accéder visiter le site www.php.net

Support du web, outils, services, compteurs, scripts, générateurs et autres outils pour les webmasters gratuitement à 100%
Page générée en 0.001916 secondes.