About PDO transaction function

autoload
Release: 2023-04-09 19:42:01
Original
3084 people have browsed it

Definition: PDOThe transaction function is not an additional function of PDO, but a certain modification of the transaction operations originally supported by MySQL Encapsulation implementation. Note: Whether the transaction execution is successful is determined by whether the corresponding storage engine of MySQL supports it.

1. Transaction function review: Transaction refers to changing the default mechanism of writing to the data table in one operation. Instead, it records the operation through the transaction log and writes it in a one-time operation at the end. to the datasheet.

  • Start transaction: start transaction, the write operation stops writing directly to the data table, but is recorded to the transaction log

  • Transaction operation: specific write operation, usually multiple steps and multiple instructions

  • Commit transaction: that is, the transaction operation ends

    Successful submission: commit, synchronize all transaction log contents to the data table, and clear the current transaction log

                Failed rollback: rollback, directly clear the current transaction log

2. The PDO class provides a set of solutions to implement transaction operations

<?php
$pdo = new PDO(&#39;mysql:host=localhost;port=3306;dbname=my_database&#39;,&#39;root&#39;,&#39;root&#39;);
$pdo->beginTransaction() or die(&#39;事务开启失败&#39;);//开启事务
$pdo->exec(&#39;insert into student values()&#39;);//执行事务

//终止事务
$pdo->commit();				//成功提交
$pdo->rollback();			//失败回滚
Copy after login

3. In transaction operations, there is a Rollback point mechanism is not implemented in PDO. If necessary, it can be implemented through SQL command settings

<?php
$pdo = new PDO(&#39;mysql:host=localhost;port=3306;dbname=my_database&#39;,&#39;root&#39;,&#39;root&#39;);
$pdo->beginTransaction() or die(&#39;事务开启失败&#39;);//开启事务
$pdo->exec(&#39;insert into student values()&#39;);//执行事务
//设置回滚点
$pdo->exec(&#39;savepoint sp1&#39;);

//继续执行事务...

//回滚
$pdo->exec(&#39;rollback to sp1&#39;);

//终止事务
$pdo->commit();				//成功提交
$pdo->rollback();			//失败回滚
?>
Copy after login

Recommendation: php tutorial,php video tutorial

The above is the detailed content of About PDO transaction function. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!