Detailed explanation of PDO::beginTransaction usage

藏色散人
Release: 2023-04-05 19:34:01
Original
3006 people have browsed it

This article mainly introduces you to the detailed usage of PDO::beginTransaction. I hope it will be helpful to friends in need!

Detailed explanation of PDO::beginTransaction usage

PDO::beginTransaction(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)

PDO::beginTransaction starts a transaction.

Syntax description:

PDO::beginTransaction ( void ) : bool
Copy after login

Turn off automatic submission mode. When autocommit mode is turned off, changes made to the database through PDO object instances are not committed until PDO::commit() is called to end the transaction. Calling PDO::rollBack() rolls back changes made to the database and returns the database connection to autocommit mode.

Some databases, including MySQL, will automatically commit an implicit transaction when issuing a DDL statement such as DROP TABLE or CREATE TABLE. Implicitly committing will prevent you from rolling back any other changes within the scope of this transaction.

Return value:

Returns TRUE on success, or FALSE on failure.

Code example

Rolling back a transaction

The following example rolls back this change Before starting a transaction and issuing two statements that modify the database. But in MySQL, the 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 = &#39;hamburger&#39;");
/*  识别出错误并回滚更改 */
$dbh->rollBack();
/* 数据库连接现在返回到自动提交模式 */
?>
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of Detailed explanation of PDO::beginTransaction usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template