CodeIgniter Forums
num_rows() from CI3 on CI4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: num_rows() from CI3 on CI4 (/showthread.php?tid=76210)



num_rows() from CI3 on CI4 - joseCarlos - 04-22-2020

Hi again,
I have been having problems in verifying if a database record exists by selecting it and returning his row, and then proceed to count the number of rows deciding true(if number of rows bigger than 0) and false(else)

Can you give me a hint, I feel like I'm missing something really simple, and I am typing unnecessary code.
Thank you, the your time.

PHP Code:
public static function verify_user_by_cod($cod)
  {

    $db db_connect();
    $builder $db->table('users');

    $builder->select('*');
    $builder->where('cod_user'$cod);
    $query $builder->get();

    
    
if (count($query->getResultArray()) > 0) {
      return true;
    } else {
      return false;
    }
  



RE: num_rows() from CI3 on CI4 - jreklund - 04-22-2020

Hi you have a countAllResults() function you can use instead. You can ignore select() and get() and only use table(), where() and countAllResults().


RE: num_rows() from CI3 on CI4 - joseCarlos - 04-22-2020

(04-22-2020, 10:15 AM)jreklund Wrote: Hi you have a countAllResults() function you can use instead. You can ignore select() and get() and only use table(), where() and countAllResults().

Thank very much jreklund, i did spend time looking around in the documentation...ehehe seems i lack focus.

The code looks like below and works without a problem, thank you.
PHP Code:
    $db db_connect();
    $builder $db->table('users');
    $builder->where('cod_user'$cod);
    
    
if ($builder->countAllResults() > 0) {
      return true;
    } else {
      return false;
    



RE: num_rows() from CI3 on CI4 - jreklund - 04-22-2020

Great that it worked out for you, and thanks for providing the finished code. It will help others develop their applications in the future.


RE: num_rows() from CI3 on CI4 - sneakyimp - 12-28-2020

(04-22-2020, 11:08 AM)jreklund Wrote: Great that it worked out for you, and thanks for providing the finished code. It will help others develop their applications in the future.
It seems bad that there is no function to tell us how many rows were fetched by a query that we have already run. PHP has a function doing just that for MySQLi, Postgres, and MSSQL.