Welcome Guest, Not a member yet? Register   Sign In
DB doesn't have num_rows/numRows ??
#1

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');

if( 
$q->num_rows() == )

 
   $row $q->row(); 



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?
Reply
#2

(09-15-2018, 10:51 AM)skunkbad Wrote: 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');

if( 
$q->num_rows() == )

 
   $row $q->row(); 



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?

Try $q->numRows
Reply
#3

(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:

PHP Code:
$q $this->db->query('SELECT * FROM foo LIMIT 1');

if( 
$q->num_rows() == )

 
   $row $q->row(); 



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?

Try $q->numRows

I will try it once I actually install CI4. I just didn't see anything in the documentation, so thought I'd ask.
Reply
#4

(This post was last modified: 09-15-2018, 03:33 PM by dannywebdev.)

$usersCount = $UsersModel->countAllResults();

https://bcit-ci.github.io/CodeIgniter4/d...ng-results
Reply
#5

(This post was last modified: 09-15-2018, 04:47 PM by skunkbad.)

(09-15-2018, 03:29 PM)dannywebdev Wrote: $usersCount = $UsersModel->countAllResults();

https://bcit-ci.github.io/CodeIgniter4/d...ng-results

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 ....
Reply
#6

(09-15-2018, 04:37 PM)skunkbad Wrote:
(09-15-2018, 03:29 PM)dannywebdev Wrote: $usersCount = $UsersModel->countAllResults();

https://bcit-ci.github.io/CodeIgniter4/d...ng-results

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 ....

See https://github.com/bcit-ci/CodeIgniter4/pull/109
Reply
#7

(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.
Reply
#8

(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

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.

We appreciate you leaving out the harsh rebuke's unless you have the data to back it up. Smile 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. Smile

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.
Reply
#9

(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

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.

We appreciate you leaving out the harsh rebuke's unless you have the data to back it up. Smile 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. Smile

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.

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().
Reply
#10

(This post was last modified: 12-28-2020, 04:40 PM by sneakyimp.)

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB