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

PDO::commit

PDO::commit

PHP PDO Reference ManualPHP PDO Reference Manual

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


Description

Syntax


bool PDO::commit ( void )

Commits a transaction, and the database connection returns to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction.


Return Value

Returns TRUE on success, or FALSE on failure.


Example

Commit a basic transaction


beginTransaction();

/* Insert multiple records based on all-or-nothing principle (either insert all, or insert 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();

/* Database connection returns to auto-commit mode */
?>

Commit a DDL transaction


beginTransaction();

/* Change the database schema */
$sth = $dbh->exec("DROP TABLE fruit");

/* Change database schema */
$dbh->commit();

/* Database connection returns to auto-commit mode */
?>

**Note:** Not all databases allow transactional operations with DDL statements: some produce errors, while others (including MySQL) auto-commit the transaction upon encountering the first DDL statement.


PHP PDO Reference ManualPHP PDO Reference Manual