文字

PDO::commit

(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)

PDO::commit 提交一个事务

说明

bool PDO::commit ( void )

提交一个事务,数据库连接返回到自动提交模式直到下次调用 PDO::beginTransaction() 开始一个新的事务为止。

返回值

成功时返回 TRUE , 或者在失败时返回 FALSE

范例

Example #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 ();


?>

Example #2 提交一个DDL事务

<?php

$dbh -> beginTransaction ();


$sth  $dbh -> exec ( "DROP TABLE fruit" );


$dbh -> commit ();


?>

Note: 并不是所有数据库都允许使用DDL语句进行事务操作:有些会产生错误,而其他一些(包括MySQL)会在遇到第一个DDL语句后就自动提交事务。

参见

  • PDO::beginTransaction() - 启动一个事务
  • PDO::rollBack() - 回滚一个事务
  • 事务和自动提交

用户评论:

[#1] re_action [2015-03-03 12:52:16]

Keep in mind this bug: https://bugs.php.net/bug.php?id=66528

you could not rely on commit() return value while using MySql

[#2] Dusan [2014-05-19 19:22:38]

Note that this will raise the PDOException on error EVEN if the error handling of PDO is set to PDO::ERRMODE_WARNING.

上一篇: 下一篇: