代码之家  ›  专栏  ›  技术社区  ›  Timothy Coetzee

数据库类型int使用值进行计算

  •  3
  • Timothy Coetzee  · 技术社区  · 7 年前

    我在codeigniter平台上工作(学习)。除了mysql语法之外,代码与普通php没有太大的不同

    问题

    用户余额为100美元。然后他报名参加比赛。比赛的入场费是50美元。因此:

    1. 从数据库中获取余额
    2. 从数据库中检索的值减去5$0
    3. 用新值更新数据库。

    上面应该很简单,但我得到了奇怪的结果。

    GAMESY模型

    public function get_balance($userID)
        {
            $sql = "SELECT balance FROM users WHERE userID = '$userID'";
            $stmnt = $this->db->query($sql);
            if($stmnt->num_rows() > 0){
               $balance =  $stmnt->result();
               return $balance;
        }
    return false;
        }
    
        public function update_balance($userID){
            //get balance
            $balance = $this->get_balance($userID);
            //charge for pool entry
            $newBalance = (int)$balance - (int)50;
            var_dump($newBalance);
            echo '<h1>'.$newBalance.'</h1>';
            $this->db->set('balance', (int)$newBalance);
            $this->db->where("userID", $userID); //table column field, second argument
            $this->db->update('users');
    
        }
    

    游戏控制器

    //Charge for picks #TODO add a check if user has enough funds to enter!!!!!
                echo $this->games_model->update_balance($this->session->userID);
                //Transferring data to Model uploaded
               echo $this->games_model->record_picks($data['picks']);
    
                $this->load->view('templates/header', $data);
                $this->load->view('games/record_picks', $data);
                //$this->load->view('templates/upcoming_fixtures_tbl', $data['games']);
                $this->load->view('templates/footer', $data);
            }
    

    我做了什么

    类型 balance 在DB类型中 int() 然而,得到这个值并减去50得到的结果是错误的。然后我换了 平衡 字段到类型 varchar() 试图减去50。仍然是错误的结果。

    最后,我尝试了类型转换,正如您在上面的代码中看到的,但是它仍然产生错误的结果。

    我得到的结果

    在这个例子中,我得到用户的 平衡 是150。然后我尝试从中减去50。我得到的结果是… -49 真奇怪。

    非常感谢您的帮助。

    更新 :

    我已经调试了这个方法 get_balance() 可以确认正确的余额被检索。 问题发生在 update_balance() 方法。

    更新2:

    当我试图 echo $balance=$this->在 更新_balance() 方法,获取数组到字符串的转换。所以我怀疑这就是问题所在。

    严重性:通知消息:数组到字符串转换文件名: models/games_model.php行号:130

    更新3

    var_dump() 方法的 获得平衡()

    数组(大小=1)0=> 对象(stdclass)[41] 公共“balance”=>字符串“-49”(长度=3)

    1 回复  |  直到 7 年前
        1
  •  3
  •   Pradeep    7 年前

    希望这对你有帮助:

    注释 :设置字段类型 int 而不是打字 balance 表中的列

    改为返回单行。你的 get_balance 应该是这样的:

    public function get_balance($userID)
    {
      $this->db->select('balance');
      $this->db->from('users');
      $this->db->where('userID',$userID);
      $query = $this->db->get();
      if ($query->num_rows() > 0)
      {
         return $query->row()->balance;
      }
    
      return false;
    }
    

    你的 update_balance 方法应该如下:

    public function update_balance($userID)
    {
        $balance = $this->get_balance($userID);
    
        $newBalance = ! empty($balance) ? ($balance - 50) : NULL;
        var_dump($newBalance);
        echo '<h1>'.$newBalance.'</h1>';
    
        $this->db->where("userID", $userID); 
        $this->db->update('users',['balance' => $newBalance]);
    }