需要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' => '用户不存在!']);
}
}
}