• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
count all result from a table using where clause

#1
[eluser]ibnclaudius[/eluser]
I know how to do it using num_rows(), but how can I do this using count_all()?

I tried this, but no success:

Code:
public function count_school_update_comments($update_id)
{
  $query = $this->db->count_all($this->school_updates_comments_table)
    ->where("update_id", $update_id);

  return $query;
}

#2
[eluser]ibnclaudius[/eluser]
I was checking the docs. Is that possible? Or only with num_rows()? I want to do it with count_all because I think will use less memory than num_rows(), because with one I just count, and the other I select everything and then count..

#3
[eluser]CroNiX[/eluser]
Either way, it still has to query the database and count the results. Do both ways and run the profiler and see which is more efficient. Not sure if you can use a where() with count_all_results(). Check the code for count_all_results(). Its doing a COUNT(field) and returning the num_rows().

#4
[eluser]aquary[/eluser]
As long as the the condition are good, using DB's count() is always faster than PHP's count() or CI's num_rows(), which require the data to be sent back to PHP for counting.

And you are putting a wrong code/order. The where should be in front of/before the counting part. The way you did would be... "count everything, then assign a condition for the next query" ?

Code:
$this->db
   ->where('condition', 'value')
   ->count_all_results('table');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.