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

Php 数据库类型int使用值进行计算,php,codeigniter,mysqli,codeigniter-3,Php,Codeigniter,Mysqli,Codeigniter 3,我正在CodeIgniter平台上工作(学习)。除了mysql语法外,该代码与普通PHP没有太大区别 问题 一个用户有100美元的余额。然后他报名参加一场比赛。锦标赛的入场费是50美元。因此: 从DB获得平衡 从数据库中检索的值减去5$0 用新值更新数据库 以上应该是相当简单的,但我得到了奇怪的结果 博弈模型 我所做的 db中的balance类型为int(),但是获取该值并减去50会得到错误的结果。然后我将balance字段更改为键入varchar(),并尝试减去50。仍然是错误的结果 最后我尝

我正在CodeIgniter平台上工作(学习)。除了mysql语法外,该代码与普通PHP没有太大区别

问题

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

  • 从DB获得平衡
  • 从数据库中检索的值减去5$0
  • 用新值更新数据库 以上应该是相当简单的,但我得到了奇怪的结果

    博弈模型 我所做的

    db中的
    balance
    类型为
    int()
    ,但是获取该值并减去50会得到错误的结果。然后我将
    balance
    字段更改为键入
    varchar()
    ,并尝试减去50。仍然是错误的结果

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

    我得到的结果

    在本例中,我得到了用户的
    余额
    ,即150。然后我尝试从中减去50。我得到的结果是…
    -49
    ,这真的很奇怪

    非常感谢您的帮助

    更新

    我已经调试了方法
    get_balance()
    ,可以确认检索到了正确的余额。问题发生在
    update\u balance()
    方法中

    更新2:

    当我尝试
    echo
    $balance=$this->get_balance($userID);在
    update\u balance()
    方法中,我得到了一个数组到字符串的转换。所以我怀疑这就是问题所在

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

    更新3

    方法的
    get\u balance()

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


    希望这对您有所帮助:

    注意:设置字段类型
    int
    ,而不是在表中键入
    余额

    返回单行。您的
    get_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]);
    }
    
    您的
    update\u 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]);
    }
    
    公共函数更新\u余额($userID)
    {
    $balance=$this->get_balance($userID);
    $newBalance=!空($balance)?($balance-50):空;
    var_dump($newBalance);
    回声“.$newBalance.”;
    $this->db->where(“userID”,$userID);
    $this->db->update('users',['balance'=>$newBalance]);
    }
    
    当您在
    update\u balance()中回显变量时
    是否得到预期结果?或者是更新查询未按预期工作?我可以确认更新查询正在工作,因为它使用
    49
    更新
    balance
    。让我
    echo
    内部的一些变量
    更新\u余额
    并报告back@Mike请参阅更新2您应该使用整数类型(smallint、int、bigint)或浮点。您不应该使用varchar来存储数字!我知道我只是试着用它来调试。非常感谢。快乐是我的快乐
    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;
    }
    
    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]);
    }