请教我在php里这样使用try是否正确

在写一个开奖的时候,在开奖的时候,将中奖信息存入了redis,然后又写了如下代码调用,请教下使用方式是否正确

    public function giveOutGoldFoins()
    {
        $object = new cache();
        $userObj = new user();
        $UserFundsLog = new UserFundsLog();
        $cathectic = new cathectic();
        while (true) {
            try{
                $data = $object->lpop('win_the_lottery');
                if(false == $data){
                    break;
                }
                $logData = json_decode($data,true);
                //将金币分配给用户,修改用户余额
                $sql = 'UPDATE `ct_user` SET `gold` = gold+'.$logData['money'].'  WHERE `id` = '.$logData['user_id'];
                $result = $userObj->updateData($sql,[]);
                //修改本期记录
                $sql = 'UPDATE `ct_cathectic` SET `state`=1 WHERE id='.$logData['id'];
                $res = $cathectic->updateData($sql,[]);
                //记录中奖消息
                $log = [
                    ':user_id' => $logData['user_id'],
                    ':classify'=> MONEY_FLOW_LOTTERY,
                    ':number'  => $logData['money'],
                    ':before_num' => $logData['gold'],
                    ':after_num'  => $logData['gold']+$logData['money'],
                    ':explain'    => '恭喜您,第:'.$logData['issue'].'期特码,中奖'.$logData['money'].'金币',
                    ':data'          => $logData['data'],
                    ':create_time'=> time(),
                ];
                $sql = 'INSERT INTO `lottery`.`ct_user_funds_log` (`user_id`, `classify`, `number`, `before_num`, `after_num`, `explain`, `data`, `create_time`) VALUES (:user_id, :classify, :number, :before_num, :after_num, :explain, :data, :create_time)';
                $UserFundsLog->addData($sql,$log);
            }catch(Exception $e){
                file_put_contents('开奖错误记录.log',$e->getMessage().PHP_EOL, FILE_APPEND);
            }
        }
    }
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题