Welcome Guest, Not a member yet? Register   Sign In
Database Num_rows()?
#7

(03-02-2020, 03:38 PM)tweenietomatoes Wrote:
(03-02-2020, 03:21 PM)stlake2011 Wrote: Thanks, I knew I was probably missing something somewhere.


For anyone remotely interested as it appears from what I have read, that there is no Num_Rows() function in CI 4, I have thrown together a quick and dirty function you can include in your models if needed. 

Note: I did NOT include any checking if the $table param was present or not, that you can write specifically for yourself.
Also only tested this for MySQL so other Databases I cannot vouch for. 

Code:

Code:
private function NumRows($table)
{
    //SQL for Counting rows in a query
    $sql = "SELECT COUNT(*) as Count FROM ".$table;

    //Execute the query and assign the result to the $row variable
    $result = $this->db->query($sql);
    $row = $result->getRow();
       
    //Get the count from the $row variable and return the result to the caller
    return $count = $row->Count;
}

countAllResults and countAll do the same thing for you. I guess num_rows was performance killer so they removed it.

If I was using the query builder (which I am not) then these work great, but i need something that I can use working directly with old fashioned SQL.
Reply


Messages In This Thread
Database Num_rows()? - by stlake2011 - 03-02-2020, 11:15 AM
RE: Database Num_rows()? - by tweenietomatoes - 03-02-2020, 12:48 PM
RE: Database Num_rows()? - by abatrans - 03-02-2020, 01:04 PM
RE: Database Num_rows()? - by InsiteFX - 03-02-2020, 02:18 PM
RE: Database Num_rows()? - by stlake2011 - 03-02-2020, 03:21 PM
RE: Database Num_rows()? - by tweenietomatoes - 03-02-2020, 03:38 PM
RE: Database Num_rows()? - by stlake2011 - 03-02-2020, 04:09 PM
RE: Database Num_rows()? - by kilishan - 03-02-2020, 09:23 PM
RE: Database Num_rows()? - by sneakyimp - 12-28-2020, 04:32 PM
RE: Database Num_rows()? - by nonebeliever - 03-03-2020, 02:55 AM
RE: Database Num_rows()? - by lukmim - 07-20-2021, 12:49 AM
RE: Database Num_rows()? - by ojmichael - 12-29-2020, 04:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB