Some programs need to perform multiple actions when executing, and our business requirement is that when an action is executed incorrectly, all actions of the process will not When executing again, it will be considered successful only if all executions are successful. Otherwise, it will return to the state before execution, which requires transaction processing. (Recommended learning: PHP video tutorial)
The native code is as follows:
<?php $link = mysqli_connect('localhost','username','password'); //创建链接 if(!$link) exit('数据库连接失败'); //判断是否链接成功 mysqli_set_charset($link , 'utf8'); //设置字符集 mysqli_select_db($link,'myDatabase'); //选择数据库 $sql1 = "正确的插入语句"; //准备sql语句 $sql2 = "错误的插入语句"; $result1 = mysqli_query($link , $sql1); //发送sql语句 $result2 = mysqli_query($link , $sql2); if($result1 && $result2) { mysql_query(“COMMIT”); //提交事务 echo "提交成功"; } else { mysql_query("ROLLBACK"); echo '数据回滚'; } mysql_query("END"); //事务结束
PDO code is as follows:
try { //实例化PDO对象 $pdo = new PDO("mysql:host=localhost;dbname=test","root","root",array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //开启事务 $pdo->beginTransaction(); //执行sql语句 $pdo->exec("insert into t1(username,password,rtime,rip) values('shiwu2','shiwu2','456456456','456456456')"); $pdo->exec("insert into t2(username,password,rtime,rip) values('shiwu2',shiwu2','456456456','456456456')"); //提交事务 $pdo->commit(); //PDO PDOStatement PDOException } catch(PDOException $e) { //回滚事务 $pdo->rollBack(); echo "数据回滚"; }
Using transactions in TP5 framework
//模型方法 function demo{ //开启事务 $this->startTrans(); //业务逻辑操作 $data['id'] = 1; $res = $this->insertUserInfo($data); //保存用户信息 if($res) { //提交事务 $this->commit(); return $res; } else { //事务回滚 $this->rollback(); } }
The above is the detailed content of When does php use transactions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
