在codeigniter中,我需要以这样的方式使用count

时间:2012-04-15 17:37:21

标签: mysql codeigniter

我需要调用一个查询并获取一些事件的数量。换句话说,我需要一个CodeIgnıter方法,以便查询产生类似的东西:

SELECT COUNT( * ) AS total FROM comments WHERE `level`= 4

5 个答案:

答案 0 :(得分:7)

$this->db->select('count(*)');
$this->db->from('comments');
$this->db->where('level','4');
$query = $this->db->get();
echo $query->num_rows();

答案 1 :(得分:3)

    $this->db->select('count(*) as total');  
    $this->db->from('comments');
    $this->db->where('level', 4);
    $sql   = $this->db->return_query();
    /*debug*/ 
    /*echo 'sql:: '.$sql;*/
    $query = $this->db->get(); 
    $arr   = $query->row_array(); 
    $total = $arr['total'];  

答案 2 :(得分:2)

$query = $this->db->query("SELECT COUNT(*) AS total FROM comments WHERE `level`= 4");
echo $query->result_array();

$this->db->query("SELECT COUNT(*) AS total FROM comments");
$this->db->where("`level`= 4");

答案 3 :(得分:1)

您可以随时使用

$this->db->query('SELECT COUNT( * ) AS total FROM comments WHERE `level`= 4');

答案 4 :(得分:1)

我想你会喜欢这样的:

$count = $this->db->where("level","4")->count_all_results("comments");

http://codeigniter.com/user_guide/database/active_record.html