Search code examples
mysqlcodeigniteractiverecordcount

Count rows with a specific column value using CodeIgniter's ActiveRecord


How can I write the following SQL using CodeIgniter's query builder methods?

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

Solution

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