Latest web development tutorials

PDO :: commit

PHP PDO Reference Manual PHP PDO Reference Manual

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


Explanation

grammar

bool PDO::commit ( void )

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


return value

Successful return TRUE, or on failure returns FALSE.

Examples

Submit a transaction basis

<?php
/* 开始一个事务,关闭自动提交 */
$dbh->beginTransaction();

/* 在全有或全无的基础上插入多行记录(要么全部插入,要么全部不插入) */
$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,
    ));
}

/* 提交更改 */
$dbh->commit();

/* 现在数据库连接返回到自动提交模式 */
?>

Submit a DDL transaction

<?php
/*  开始一个事务,关闭自动提交 */
$dbh->beginTransaction();

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

/* 更改数据库架构 */
$dbh->commit();

/* 现在数据库连接返回到自动提交模式 */
?>

Note: Not all databases allow the use of DDL statement transaction: some will generate an error, and some (including MySQL) Other transaction will automatically be submitted in a DDL statement after the first encounter.


PHP PDO Reference Manual PHP PDO Reference Manual