DB doesn't have num_rows/numRows ?? |
In CodeIgniter 3 we could run a query then find if there were rows returned:
PHP Code: $q = $this->db->query('SELECT * FROM foo LIMIT 1'); In CI4 I don't see a way to check the number of rows, or if there was a row at all. How are you handling this? Can we get a numRows() please?
(09-15-2018, 10:51 AM)skunkbad Wrote: In CodeIgniter 3 we could run a query then find if there were rows returned: Try $q->numRows (09-15-2018, 12:24 PM)Paradinight Wrote:(09-15-2018, 10:51 AM)skunkbad Wrote: In CodeIgniter 3 we could run a query then find if there were rows returned: I will try it once I actually install CI4. I just didn't see anything in the documentation, so thought I'd ask.
(09-15-2018, 03:29 PM)dannywebdev Wrote: $usersCount = $UsersModel->countAllResults(); No, I don't want to do a count query. I want the functionality of CI3's num_rows(), and if it exists I want it documented, and if it's documented I'd like to know where it is. Also, now looking at the code, it looks like there is a $q->numRows, but I'm not so sure this is the same thing as CI3's num_rows(). Am I right that in CI2 one would use num_rows, then in CI3 it became a function, and now in CI4 who knows ....
(09-15-2018, 04:37 PM)skunkbad Wrote:(09-15-2018, 03:29 PM)dannywebdev Wrote: $usersCount = $UsersModel->countAllResults(); See https://github.com/bcit-ci/CodeIgniter4/pull/109 (09-15-2018, 10:11 PM)Paradinight Wrote: See https://github.com/bcit-ci/CodeIgniter4/pull/109 Quote:I had left the numRows out on purpose. We can always discuss whether to provide it or not but we had been discouraging its use for the last couple of years and writing it out of the user guide examples due to performance and memory issues on several platforms. Performance and memory issues? I will refrain from issuing a harsh rebuke, but I willl say that it sounds like CI4 must have some kind of problem that CI3 didn't have. Either that or these "performance and memory issues" are insignificant and being blown out of proportion. Quote:All getResult methods will return an empty array and all getRow methods return null if the record doesn't exist. I can live with that, but still believe that there should be a getNumRows() method. (09-16-2018, 08:12 AM)skunkbad Wrote:(09-15-2018, 10:11 PM)Paradinight Wrote: See https://github.com/bcit-ci/CodeIgniter4/pull/109 We appreciate you leaving out the harsh rebuke's unless you have the data to back it up. The performance issues were being seen in CI3, and likely 2 also, but not in all database drivers. CI3 supported a vast amount of databases, and not all of them supported the features needed to do some of those, so the only way to issue a count of results was to pull them all out at once and count the resulting array, while other databases supported using a cursor so when you looped over results it was much faster and more memory-efficient. I don't recall which were which, and wasn't involved in CI development when those problems were found, so that's about as far as I can comment on that. As it stands at the moment, we only have 3 databases supported, so might re-visit that. But currently focused on just getting Alpha out the door. But as that diff pointed out - you can tell if you have results by checking the response itself. If you need to know the number of results exactly, grab the results and count the returned array. Simple. (09-16-2018, 11:42 AM)kilishan Wrote:(09-16-2018, 08:12 AM)skunkbad Wrote:(09-15-2018, 10:11 PM)Paradinight Wrote: See https://github.com/bcit-ci/CodeIgniter4/pull/109 I usually just need to know if there was anything returned, and in some cases I check for 1. Not knowing exactly how the DB libraries work under the hood, I guess I just assumed it was a simple count() of the results. I'm all for allowing CI4 to be the fastest it can be, and while the stats/data might be interesting to take a look at, I guess I'll just take your word for it. It's not a show stopper to be without num_rows().
I'd also like to see any analysis detailing these 'performance problems' in the most commonly used PHP databases. I might be mistaken, but I'd wager the vast majority of CodeIgniter installations use MySQL/MariaDB, PostgreSQL, or MSSQL and all these DB extensions offer a numrows function.
EDIT: Quote:But as that diff pointed out - you can tell if you have results by checking the response itself. If you need to know the number of results exactly, grab the results and count the returned array. Simple.This is too simple. I have one query which might return a very large result set with several thousand records. It would be a shame to waste CPU and memory power instantiating this simply to find out how many records were returned. |
Welcome Guest, Not a member yet? Register Sign In |