CodeIgniter Active Record - Get number of returned rows

ActiverecordCodeigniter

Activerecord Problem Overview


I'm very new to CodeIgniter and Active Record in particular, I know how to do this well in normal SQL but I'm trying to learn.

How can I select some data from one of my tables, and then count how many rows are returned using CodeIgniters Active Record class?

Thanks, Tom.

Activerecord Solutions


Solution 1 - Activerecord

Have a look at the result functions here:

$this->db->from('yourtable');
[... more active record code ...]
$query = $this->db->get();
$rowcount = $query->num_rows();

Solution 2 - Activerecord

AND, if you just want to get a count of all the rows in a table

$table_row_count = $this->db->count_all('table_name');

Solution 3 - Activerecord

This goes to you model:

public function count_news_by_category($cat)
{
	return $this->db
		->where('category', $cat)
		->where('is_enabled', 1)
		->count_all_results('news');
}

It'a an example from my current project.

According to benchmarking this query works faster than if you do the following:

$this->db->select('*')->from('news')->where(...); 
$q = $this->db->get(); 
return $q->num_rows();

Solution 4 - Activerecord

If you only need the number of rows in a query and don't need the actual row data, use count_all_results

echo $this->db
       ->where('active',1)
       ->count_all_results('table_name');

Solution 5 - Activerecord

Just gotta read the docs son!

$query->num_rows();

Solution 6 - Activerecord

You can do this in two different ways:

  1. $this->db->query(); //execute the query
     $query = $this->db->get() // get query result
     $count = $query->num_rows() //get current query record.

  2.  $this->db->query(); //execute the query
      $query = $this->db->get() // get query result
      $count = count($query->results()) 
          or   count($query->row_array())  //get current query record.

Solution 7 - Activerecord

$this->db->select('count(id) as rows');
$this->db->from('table_name');
$this->db->where('active',1);
$query = $this->db->get();
foreach($query->result() as $r)
{
   return $r->rows;
}

Solution 8 - Activerecord

This is also a very useful function if you are looking for a rows or data with where condition affected

function num_rows($table)
	{   
	   return $this->db->affected_rows($table);
	}

Solution 9 - Activerecord

This code segment for your Model

function getCount($tblName){
    $query = $this->db->get($tblName);
    $rowCount = $query->num_rows();
    return $rowCount;       
}

This is for controlr

  public function index() {
    $data['employeeCount']= $this->CMS_model->getCount("employee");
    $this->load->view("hrdept/main",$data);
}

This is for view

<div class="count">
  <?php echo $employeeCount; ?>                                                                                            
 </div>

This code is used in my project and is working properly.

result

Solution 10 - Activerecord

function getCount(){
    return $this->db->get('table_name')->num_rows();
}

Solution 11 - Activerecord

$sql = "SELECT count(id) as value FROM your_table WHERE your_field = ?";
$your_count = $this->db->query($sql, array($your_field))->row(0)->value;
echo $your_count;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZimView Question on Stackoverflow
Solution 1 - ActiverecordResiduumView Answer on Stackoverflow
Solution 2 - ActiverecordZackView Answer on Stackoverflow
Solution 3 - ActiverecordYura LoginovView Answer on Stackoverflow
Solution 4 - ActiverecordpbarneyView Answer on Stackoverflow
Solution 5 - ActiverecordascotanView Answer on Stackoverflow
Solution 6 - Activerecordprash.patilView Answer on Stackoverflow
Solution 7 - ActiverecordBijendra ChView Answer on Stackoverflow
Solution 8 - ActiverecordNishant LadView Answer on Stackoverflow
Solution 9 - ActiverecordLuckyView Answer on Stackoverflow
Solution 10 - ActiverecordSunday OkoiView Answer on Stackoverflow
Solution 11 - ActiverecordJohn ErckView Answer on Stackoverflow