【问题标题】:unable to count the result with a condition using codeigniter query无法使用 codeigniter 查询使用条件计算结果
【发布时间】:2013-01-31 09:31:29
【问题描述】:

我正在尝试计算以 display_id 作为参数的 bookdetails 表的行数。假设我通过了$id='3'。但我没有得到输出。 我认为我正在尝试的代码是错误的。请帮助我正确编写此查询

  //--- Counting the rows of bookdetails of table where display_id is as argument------------------------------------------------------------- 
 public function record_count_for_secondtopBooks($id) {
     $this->load->database(); 
    return $this->db->count_all("bookdetails",array('display_id'=>$id)); 
}

【问题讨论】:

    标签: php mysql codeigniter codeigniter-3


    【解决方案1】:

    count_all 返回特定的行数

    echo $this->db->count_all('my_table');
    

    试试这个

    $this->db->where('display_id', $id);
    $this->db->from('bookdetails"');
    $this->db->count_all_results();
    

    【讨论】:

      【解决方案2】:

      count_all 只接受一个参数,即表名。因此,您将获得该表中所有记录的计数。如手册中所述:

      允许您确定特定表中的行数。 在第一个参数中提交表名。示例:

      【讨论】:

        【解决方案3】:
        $this->db->where('display_id',$id);
        $result = $this->db->count_all("bookdetails");
        

        或Chain em'

        $result = $this->db->where('display_id',$id)->count_all("bookdetails");
        

        检查:

        echo'<pre>';
        print_r($result);
        echo'</pre>';
        

        【讨论】:

          【解决方案4】:

          试试这个,

          $this->db->where('display_id', $id);
          $query = $this->db->count_all('bookdetails');
          
          return $query;
          

          【讨论】:

          • 你检查过你的 $id 有价值吗?你有这个值的记录吗?
          • 我想返回计数值。我这样做是为了分页
          【解决方案5】:

          请尝试以下代码

           public function record_count_for_secondtopBooks($id) {
          
          $this->db->where('display_id',$id);
          $q = $this->db->get('bookdetails');
          return $q->num_rows();
          }
          

          【讨论】:

            【解决方案6】:

            试试这个

            public function record_count_with_where($table_name,$column_name,$type)
              {
                $this->db->select($column_name);
                $this->db->where($column_name,$type);
                $q=$this->db->get($table_name);
                $count=$q->result();
                return count($count);
            
              }
            

            【讨论】:

              猜你喜欢
              • 2014-07-04
              • 1970-01-01
              • 1970-01-01
              • 2016-10-04
              • 1970-01-01
              • 2021-10-08
              • 1970-01-01
              • 2021-04-19
              • 2016-10-09
              相关资源
              最近更新 更多