(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDO::commit — 提交一个事务
此函数没有参数。
示例 #1 提交一个基础事务
<?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();
?>
示例 #2 提交一个DDL事务
<?php
$dbh->beginTransaction();
$sth = $dbh->exec("DROP TABLE fruit");
$dbh->commit();
?>
注意: 并不是所有数据库都允许使用DDL语句进行事务操作:有些会产生错误,而其他一些(包括MySQL)会在遇到第一个DDL语句后就自动提交事务。