php codeigniter count rows

You could use the helper function $query->num_rows()

It returns the number of rows returned by the query. You can use like this:

$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();

Use this code:

$this->db->where(['id'=>2])->from("table name")->count_all_results();

or

$this->db->from("table name")->count_all_results();


You may try this one

    $this->db->where('field1',$filed1);
    $this->db->where('filed2',$filed2);
    $result = $this->db->get('table_name')->num_rows();

If you really want to count all rows. You can use this in model function:

$this->db->select('count(*)');
$query = $this->db->get('home');
$cnt = $query->row_array();
return $cnt['count(*)'];

It returns single value, that is row count