CodeIgniter Forums
Is there no longer a need for num_rows()? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Is there no longer a need for num_rows()? (/showthread.php?tid=74174)



Is there no longer a need for num_rows()? - tgix - 08-11-2019

Looking at CI4 and working with databases I can't find a way to get the number of rows returned from a query. 
In CI3 I often did:
PHP Code:
/** @var CI_DB_result $query */
$query $this->db->get(TBL_INVITES);

if (
$query->num_rows() !== 1)
{
    return 
FALSE;
}

/** Continue processing **/ 

Should I do like this in CI4 or is there a better way?
PHP Code:
if (count($query->getResultArray()) !== 1)
{
    return 
false;




RE: Is there no longer a need for num_rows()? - dave friend - 08-12-2019

I think you're looking for $builder->countAllResults()


RE: Is there no longer a need for num_rows()? - tgix - 08-12-2019

(08-12-2019, 02:17 AM)dave friend Wrote: I think you're looking for $builder->countAllResults()

Yeah, but that queries the database again, which num_rows() in CI3 doesn't do. I guess I'll do the count() once and then keep the result as a variable.


RE: Is there no longer a need for num_rows()? - dave friend - 08-12-2019

(08-12-2019, 02:54 AM)tgix Wrote:
(08-12-2019, 02:17 AM)dave friend Wrote: I think you're looking for $builder->countAllResults()

Yeah, but that queries the database again, which num_rows() in CI3 doesn't do. I guess I'll do the count() once and then keep the result as a variable.

Digging deeper I see that BaseResult has a public property - $numRows. It appears that by using any db operation that returns a Result you should be able to do something like this.

PHP Code:
$res $db->query("YOUR QUERY")->getResult();
$count $res->numRows

I haven't confirmed my premise, only explored the source code. However, after exploring the source more deeply I'm skeptical about my correctness. I cannot find any place where $numRows is assigned a value. (I"m looking at the current state of the develop branch - last commit on Aug 11, 2019)

Even if I'm wrong that property might be a useful place to store the result of your count() call.


RE: Is there no longer a need for num_rows()? - InsiteFX - 08-13-2019

The countAll method returns

PHP Code:
return (int) $query->numRows



RE: Is there no longer a need for num_rows()? - dave friend - 08-13-2019

(08-13-2019, 04:20 AM)InsiteFX Wrote: The countAll method returns

PHP Code:
return (int) $query->numRows

But countAll performs another query on the DB which the OP wants to avoid. That, and countAll() returns the count for the entire table without considering any "where" or "like" clauses.


RE: Is there no longer a need for num_rows()? - Stormbringer - 10-18-2019

Maybe?

PHP Code:
$query->resultID->num_rows 



RE: Is there no longer a need for num_rows()? - sneakyimp - 12-28-2020

I firmly believe that the BaseResult object returned by a query should have a method to tell you how many records are in the result without having to run yet another query. Is there no such function/method?