即使有行,num_rows()也会返回1

时间:2013-11-27 18:19:05

标签: php mysql codeigniter

$this->db->select_sum('score_0')
    ->select_sum('score_1')
    ->select_sum('score_2')
          ..........
    ->select_sum('score_97')
    ->select_sum('score_98')
    ->select_sum('score_99');
    $this->db->where('score_game_id',$game_id);
    $this->db->order_by('score_id','asc');
    $query = $this->db->get($this->tbl_lotteryApp_scores_twodigit);     
    if ($query->num_rows() > 0){
        }

可能是什么原因?

2 个答案:

答案 0 :(得分:2)

即使返回的每个值都为零,总是返回一行,因为您必须按照要求返回这些零值(从命令行运行sql并查看)。您需要获取查询结果并检查其值以查看其实际值。

答案 1 :(得分:0)

我认为这是解决这种情况的最佳解决方案:

function sum_score($game_id){
    if($this->check_for_scores($game_id)==true)
    {
        $this->db->select_sum('score_0')
        ->select_sum('score_1')
        ->select_sum('score_2')
              ...........
        ->select_sum('score_98')
        ->select_sum('score_99');
        $this->db->where('score_game_id',$game_id);
        $this->db->order_by('score_id','asc');
        $query = $this->db->get($this->tbl_lotteryApp_scores_twodigit);     
        return  $query->result();
    }

}

function check_for_scores($game_id)
{
    $this->db->where('score_game_id',$game_id);
    $this->db->order_by('score_id','asc');
    $query = $this->db->get($this->tbl_lotteryApp_scores_twodigit);     
    if ($query->num_rows() > 0){    
        return true;
    }else{
        return false;
    }
}

这对我有用:)