CodeIgniter Forums
num_rows returning empty while result_array has results - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: num_rows returning empty while result_array has results (/showthread.php?tid=1622)



num_rows returning empty while result_array has results - ruiganga - 03-26-2015

Hi

I don't know if it is a bug or if I am doing something wrong. I am using CI3 RC3. While testing the code I've written originally for CI2 I've figured that $query -> num_rows was empty. The query is simple and list everything I have in a table, so the results do exists, only the num_rows is empty and I don't understand why.

I can find correctly the num_rows in the following way: $query -> result_id -> num_rows

[result_id] => mysqli_result Object
(
[current_field] => 0
[field_count] => 7
[lengths] =>
[num_rows] => 25
[type] => 0
)

[result_array] => Array
(
)

[result_object] => Array
(
)

[custom_result_object] => Array
(
)

[current_row] => 0
[num_rows] =>
[row_data] =>
)

Is this a bug or the $query -> num_rows will disappear in this update?


RE: num_rows returning empty while result_array has results - mwhitney - 03-26-2015

Use $query->num_rows() (method), not $query->num_rows (property). In CI3, if the property happens to be empty, the method will set it and return the value.

Since the property is not documented, but the method is, it's unlikely you were supposed to use the property in the first place.


RE: num_rows returning empty while result_array has results - ruiganga - 03-26-2015

Hi

Thanks a lot!! mwhitney


RE: num_rows returning empty while result_array has results - Narf - 03-26-2015

(03-26-2015, 11:23 AM)mwhitney Wrote: Use $query->num_rows() (method), not $query->num_rows (property). In CI3, if the property happens to be empty, the method will set it and return the value.

Since the property is not documented, but the method is, it's unlikely you were supposed to use the property in the first place.

Correct, the property is for internal, "caching" purposes only, which is why it's not documented. Users should only use the method.


RE: num_rows returning empty while result_array has results - ruiganga - 03-26-2015

while I was testing my registration system, I've created a lot of entries in a database. then I deleted it but I still see all the rows in the query result.

In other words. I am having a result different then the data in the database. I am still seing previously deleted results.

How can I work properly with cash so this don't happend. Where can I learn about the way Codeigniter 3 caches query results?

Thanks.


RE: num_rows returning empty while result_array has results - CroNiX - 03-26-2015

(03-26-2015, 01:44 PM)ruiganga Wrote: How can I work properly with cash so this don't happend. Where can I learn about the way Codeigniter 3 caches query results?

The userguide is pretty helpful.
http://www.codeigniter.com/userguide3/database/caching.html

Quote:Since cache files do not expire, you’ll need to build deletion routines into your application. For example, let’s say you have a blog that allows user commenting. Whenever a new comment is submitted you’ll want to delete the cache files associated with the controller function that serves up your comments. You’ll find two delete functions described below that help you clear data.



RE: num_rows returning empty while result_array has results - ruiganga - 03-26-2015

I am not using Codeigniter Cache. Is the Cache used by default?

I've realised that if I clear the browser cache and then try again it works normally. How can I prepare my app so this page is not cached by the browser?

Thanks for the tips.


RE: num_rows returning empty while result_array has results - ruiganga - 03-26-2015

Saying browser cache I mean browser data, cookies... May be Facebook is saving the first answer in a cookie and only when the cookie is deleted it works normally. May be... I only know that in CI2 this didn't happened and I am confused.


RE: num_rows returning empty while result_array has results - CroNiX - 03-26-2015

send no-cache headers


RE: num_rows returning empty while result_array has results - Narf - 03-27-2015

When I said "caching", I didn't mean it in any of the ways that you're all discussing right now.

Caching just means to save something locally, so you don't have to fetch it from somewhere else when you need it again. In that sense, when you call num_rows() method once, CI will store/cache the appropriate value in a $num_rows property so that it doesn't need to get it from the underlying low-level APIs if you call num_rows() again. This is because some PHP database extensions only allow us to get the number of rows once, or don't support such method at all and we need to fetch all the results first and do a count() on them instead.

Just use what the documentation says and don't worry about this stuff.