Latest web development tutorials

PDO :: beginTransaction

PHP PDO Reference Manual PHP PDO Reference Manual

PDO :: beginTransaction start a transaction (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)


Explanation

grammar

bool PDO::beginTransaction ( void )

Turn off autocommit mode. Autocommit mode is turned off at the same time, by PDO object instance changes made to the database until you call the PDO :: commit () to end the transaction was only submitted.

Calling PDO :: rollBack () will roll back the changes made to the database and the database connection returns to autocommit mode.

Some databases including MySQL, when a similar issue DROP TABLE or CREATE TABLE DDL statements such, it will automatically be an implicit transaction commits.

Implicitly or explicitly committed will prevent you roll back any other changes within the scope of this transaction.


return value

Successful return TRUE, or on failure returns FALSE.

Examples

Roll back a transaction

The following example of this change before the rollback begins a transaction and issues two statements that modify the database.

However, in MySQL, DROP TABLE statement automatically commits the transaction, so that any changes in this transaction will not be rolled back.

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

/*  更改数据库架构及数据 */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert
    SET name = 'hamburger'");

/*  识别出错误并回滚更改 */
$dbh->rollBack();

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

PHP PDO Reference Manual PHP PDO Reference Manual