PDO::commit commits a transaction (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
bool PDO::commit ( void )
Commit a transaction and the database connection returns to auto-commit mode until the next call to PDO::beginTransaction() to start a new transaction.
<?php/* Start a transaction and turn off auto-commit */$dbh->beginTransaction();/* Insert multiple rows of records on an all-or-nothing basis (either all or none) */$ sql = 'INSERT INTO fruit (name, colour, calories) VALUES (?, ?, ?)';$sth = $dbh->prepare($sql);foreach ($fruits as $fruit) { $sth->execute(array( $fruit->name, $fruit->colour, $fruit->calories, ));}/* Commit changes*/$dbh->commit();/* Now connect to database Return to auto-commit mode*/?>
<?php/* Start a transaction and turn off automatic submission */$dbh->beginTransaction();/* Change the database schema */$sth = $dbh->exec("DROP TABLE fruit");/* Change the database Schema*/$dbh->commit();/* Now the database connection returns to auto-commit mode*/?>
Note: Not all databases allow transaction operations using DDL statements: some will generate errors, while others (including MySQL) will automatically commit transactions after encountering the first DDL statement.