views:

109

answers:

5

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 am using a classic MVC structure and 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.

Thanks for your input

+1  A: 

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

kitsched
Yes, I can see how I could use that in the model to return the number of rows, but then I'd have to pass the value over to the view which seems a long way to go about things?
Linker3000
--Edit ah, I see that I can actually use $result->num_rows in the view as well as on the original query in the model. Would doing this be considered OK in a view?
Linker3000
Not really. You should instead return an array from your model. One element being the count and the other one the result. I usually return row_array results from the model, btw.
kitsched
Yes that makes sense and keeps things where they are expected to be. Still not sure why count doesn't work but it's time to move on and perhaps come back to that code for more investigation later. Many thanks
Linker3000
A: 

What is output if you do a print_r or var_dump on $result.

Count seems to work on a Codeigniter site I just tried

Paul
CI_DB_mysql_result Object ( [conn_id] => Resource id #28 [result_id] => Resource id #38 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => ) . Both count syntaxes still return 1.
Linker3000
In your model are you doing $result = $this->db->query($query);return $result->result();I don't get anything about the object when I do the var_dump.
Paul
$query = $this->db->get('tdba');return $query;
Linker3000
Try $query->result();
Paul
Sorry, I thought that worked but I still had the $result->num_rows code in place. If I put count($result) back in I still get 1 every time.
Linker3000
A: 

Try if(isset($result) && count($result)) on your view file then inside the if statement you can write the code you want to executed when the inserts in your db are more than 0...good luck!

rabidmachine9
The problem with count($result) is that it always returns 1, even if there are, say, 0, 7 or 70 results.
Linker3000
+3  A: 

The best thing to do in your model is the following:

$query = $this->db->something()....
...
...
if ( $query->num_rows() > 0 )
{
    return $query->result();
}
else
{
    return FALSE;
}

Then in your controller or view you would do the following:

if ( !empty($my_db_result) ) 
{
    ......
}

This process enables you to respond on the result based on the result type. If the rows could be retrieved this will return an array of which the items can be counted by PHP's count() function. Since the second block checks if the result is empty (note that "FALSE" is treated as being empty) you won't bump into any issues (e.g. when using a foreach loop) and you can specify what to do in case there were no results.

Yorick Peterse
Thanks Yorick. I have marked Kitsched's reply as my accepted answer as he suggested num_rows first, but your explanation is great for clarity.
Linker3000
No problen at all :)
Yorick Peterse
A: 

if u put count($result) in if statement then ,when it success it returns only 1,u can try $query->num_rows() in different way...

Tejas1810