PDO::commit
PHP 教程
· 2019-01-28 15:26:24
PDO::commit提交一个事务(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
说明
语法
bool PDO::commit ( void )
提交一个事务,数据库连接返回到自动提交模式直到下次调用 PDO::beginTransaction() 开始一个新的事务为止。
返回值
成功时返回 TRUE, 或者在失败时返回 FALSE。实例
提交一个基础事务
<?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();
/* 现在数据库连接返回到自动提交模式 */
?>
提交一个DDL事务
<?php
/* 开始一个事务,关闭自动提交 */
$dbh->beginTransaction();
/* Change the database schema */
$sth = $dbh->exec("DROP TABLE fruit");
/* 更改数据库架构 */
$dbh->commit();
/* 现在数据库连接返回到自动提交模式 */
?>
注意:并不是所有数据库都允许使用DDL语句进行事务操作:有些会产生错误,而其他一些(包括MySQL)会在遇到第一个DDL语句后就自动提交事务。
点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html
Transcending CSS
Andy Clarke、Molly E. Holzschlag / New Riders / November 15, 2006 / $49.99
As the Web evolves to incorporate new standards and the latest browsers offer new possibilities for creative design, the art of creating Web sites is also changing. Few Web designers are experienced p......一起来看看 《Transcending CSS》 这本书的介绍吧!