CodeIgniter Forums
strange issues with get_where.. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: strange issues with get_where.. (/showthread.php?tid=10869)



strange issues with get_where.. - El Forum - 08-15-2008

[eluser]awpti[/eluser]
mysql> SELECT * FROM (`jobs`) WHERE `times_reported` > 0;
Empty set (0.00 sec)

But..
Code:
function fetch_reported_jobs()
{
   $this->db->get_where('jobs', 'times_reported > 0');
   return $this->db->count_all_results();
}

Returns a result of 1

Result of db->last_query: SELECT * FROM (`jobs`) WHERE `times_reported` > 0

For some reason, count_all_results() is returning 1 even though it should return 0. I can't see any issue with the code in DB_active_rec.php

Thoughts?


strange issues with get_where.. - El Forum - 08-16-2008

[eluser]awpti[/eluser]
The only way to get around this seems to be to manually issue a query.

Even $this->db->where(..) returns one when it should return 0.

The query it returns via last_query returns an empty result set when run against the DB directly, but CI sees it as a result of 1. Weird!


strange issues with get_where.. - El Forum - 08-16-2008

[eluser]adamfairholm[/eluser]
awpti - Just wanted to chime in that I've also had this issue - got around it with a manual query like you did. You're not alone!


strange issues with get_where.. - El Forum - 09-23-2008

[eluser]jeffpeck[/eluser]
Perhaps the DB is drawing from cache. Try turning caching off if you have it on and try again.


strange issues with get_where.. - El Forum - 10-02-2008

[eluser]Derek Allard[/eluser]
count_all_results() isn't intended to be used after results have been brought back... that would be num_rows(), this function is intended to simply return the number of results with some restrictions. So in this case the query would be
Code:
$this->db->where('jobs', 'times_reported > 0');
   return $this->db->count_all_results();



strange issues with get_where.. - El Forum - 01-18-2009

[eluser]rlopez[/eluser]
Whew. This was causing a *LOT* of pain. Changing count_all_results to num_rows helped. Perhaps Derek's explanation should be on the documentation?

Thanks for helping, Derek


strange issues with get_where.. - El Forum - 01-18-2009

[eluser]Derek Allard[/eluser]
Code:
echo $this->db->count_all_results('my_table');
// Produces an integer, like 25

Thanks for the feedback rlopez, how could I make this more clear in the future?


strange issues with get_where.. - El Forum - 01-18-2009

[eluser]rlopez[/eluser]
I would note on the count_all_results section, that this function is not to be used to verify if at least one row was returned, and point the reader to the num_rows function instead Smile


strange issues with get_where.. - El Forum - 01-18-2009

[eluser]xwero[/eluser]
I think this is a non issue. If the idea is to get row count you use
Code:
$this->db->where('times_reported >',0);
$this->db->count_all_results();
If the idea is to get the rows
Code:
$this->db->get_where('jobs', 'times_reported > 0');
If the application requires the row count the method should return the query object instead of the result object.

I think the AR methods are descriptive enough to make it a one time mistake.