Welcome Guest, Not a member yet? Register   Sign In
num_rows() from CI3 on CI4
#1

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;
    }
  
Reply
#2

(This post was last modified: 04-22-2020, 10:16 AM by jreklund.)

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

(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;
    
Reply
#4

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

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




Theme © iAndrew 2016 - Forum software by © MyBB