I'm working in MVC CodeIgniter with MySQL and PHP. Common case - I get data from database and I want count of them. I wonder how can I do it in the most optimial way. Is it sensible to do it in one function?
I will share with You my solution and please tell me is it a good approach. Let's say we have a Model function:
public function get_results()
{
$query = $this->db->get('data');
$result = $query->result();
$count = $query->num_rows();
return [$result, $count];
}
Question isn't that silly to return both values that way?
I want to avoid creating new function num_result() executing another SQL for only count rows since I have already ran one. Is there any other effective way to take count from one query execution?