ThinkPHP5.0.x事务提交

需要innodb类型的数据库表
例子:

<?php
namespace app\index\controller;

use think\Db;

class Index
{
    public function index()
    {
        Db::startTrans();
        $check1 = Db::table('think_user')->where('id', 1)->setInc('age', 1);
        $check  = Db::table('think_user')->where('id', 2)->setDec('age', 1);
        if ($check && $check1) {
            Db::commit();
            return json(['code' => 1, 'msg' => '提交数据成功!', 'exp' => '提交数据成功!']);
        } else {
            Db::rollback();
            return json(['code' => -1, 'msg' => '提交数据失败,请联系管理员!', 'exp' => '用户不存在!']);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/hd2killers/article/details/81205637
今日推荐