PDO::commit
PDO::commit commits a transaction (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
bool PDO::commit ( void )
Commits a transaction, returning the database connection to auto-commit mode until the next call to PDO::beginTransaction() starts a new transaction.
Return Values
Returns TRUE on success, or FALSE on failure.Examples
Commit a basic transaction
<?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();
/* 现在数据库连接返回到自动提交模式 */
?>
Commit a DDL transaction
<?php
/* 开始一个事务,关闭自动提交 */
$dbh->beginTransaction();
/* Change the database schema */
$sth = $dbh->exec("DROP TABLE fruit");
/* 更改数据库架构 */
$dbh->commit();
/* 现在数据库连接返回到自动提交模式 */
?>
Note:Not all databases allow DDL statements to be used in transactions: some will generate errors, while others (including MySQL) will automatically commit the transaction after encountering the first DDL statement.
Other Extensions
PHP PDO Reference Manual