1

我正在尝试计算以 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)); 
}
4

6 回答 6

4

count_all 返回特定的行数

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

试试这个

$this->db->where('display_id', $id);
$this->db->from('bookdetails"');
$this->db->count_all_results();
于 2013-01-31T09:38:26.367 回答
1

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

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

于 2013-01-31T09:37:53.923 回答
0

试试这个,

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

return $query;
于 2013-01-31T09:37:59.880 回答
0

请尝试以下代码

 public function record_count_for_secondtopBooks($id) {

$this->db->where('display_id',$id);
$q = $this->db->get('bookdetails');
return $q->num_rows();
}
于 2013-01-31T09:38:06.927 回答
0
$this->db->where('display_id',$id);
$result = $this->db->count_all("bookdetails");

或链他们'

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

查看:

echo'<pre>';
print_r($result);
echo'</pre>';
于 2013-01-31T09:38:29.067 回答
0

试试这个

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);

  }
于 2017-06-02T09:36:49.597 回答