PHP中try{}catch{}的用法及异常处理.对数据库的事物支持


 public  function test1()
    {
        
            Db::startTrans();
            // 事务
            try{
            // 第1条数据更新id 18
            $a=Db::name('user')->where("id", 15)->update(['ledou' => 8888888888]);
            // 判断是否更新成功
                if (!$a) {
                throw new \Exception("第1条数据更新失败");
                }
                // 第2条数据更新 id 9
                $b= Db::name('order')->where('id',9)
                        ->update(['status' => 8]);
                // 判断是否更新成功
                if (!$b) {
                    throw new \Exception("第2条数据更新失败");
                    }
                    // 执行提交操作
                Db::commit();
                }catch(\Exception $e){
                // 回滚事务
                Db::rollback();
                // 获取提示信息
                dump($e->getMessage());
                
                }
                if ($a&$b) {
                
                 return json(['code' => 1, 'msg' => '成功']);
                }else{
                 return json(['code' => 0, 'msg' => '失败']);
                
                }
        
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM