Counting the number of results returned by a database query in Codeigniter

Linker3000 picture Linker3000 · Sep 8, 2010 · Viewed 61.7k times · Source

I am not having much luck detecting when a database query in Codeigniter returns zero results. I have had a good read of the notes on the PHP count function but am none the wiser!

I call the query/view as follows from the controller:

$data['result'] = $this->search_model->do_search(set_value('name'));
$data['title'] = "Search results";
$this->load->view('search_view',$data);

The view generates a results table for me OK, but when I try and trap an empty result, the count always returns 1:

I have tried if count(array($result)) and just if count($result)

So what's a good way to get the count? I'm using Fedora 13 with PHP 5.3.3 on my dev laptop.

Answer

janosrusiczki picture janosrusiczki · Sep 8, 2010

Have a look at $query->num_rows (<- clickable).