This article mainly introduces the method of executing transactions in the thinkPHP framework, and analyzes the related implementation of transaction and rollback operations in the thinkPHP framework using the startTran(), Commit() and Rollback() methods encapsulated in the model in the form of examples. For tips, friends who need them can refer to
The examples in this article describe the method of executing transactions in the thinkPHP framework. Share it with everyone for your reference, the details are as follows:
function tran(){ //定义事务成功失败的标志 $mark = true; //1. 实例化模型 $model = D('student'); //2. 开启事务处理 $model->startTrans(); //3. ls减少2000 $sql = "update student set money=money-2000 where uname='ls'"; $result = $model->execute($sql); //判断sql执行是否成功,如果失败,则将$mark改为false if(!$result){ $mark = false; } //4. zs增加2000 $sql = "update student set money=money+2000 where uname='zs'"; $result = $user->execute($sql); //判断sql执行是否成功,如果失败,则将$mark改为false if(!$result){ $mark = false; } $mark = false; //暂时强制改为false,测试回滚效果 //5. 提交事务 //判断$mark的值,为ture则提交,为false则回滚 if($mark){ $user->commit(); } else { $user->rollback(); } }
Transaction-related methods in TP: They are all defined in Model.class.php
startTran()
: Start transaction processing
Commit()
: Commit transaction
Rollback()
: Rollback
When the transactions that need to be executed are in different sql tables, you only need to execute the two sql model definitions that are different
Related recommendations:
ThinkPHP framework’s method of adding, deleting, and modifying data
Detailed explanation of the steps of connecting database with PDO of ThinkPHP framework
thinkPHP framework automatic filling principle and usage Detailed explanation of usage
The above is the detailed content of Methods for executing transactions in thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!