'How to fix error: count(): Parameter must be an array or an object that implements Countable

A PHP Error was encountered Severity: Warning

Message: count(): Parameter must be an array or an object that implements Countable

Filename: models/login_model.php

Line Number: 17

Backtrace:

File: C:\xampp\htdocs\labexercise007\application\models\login_model.php Line: 17 Function: _error_handler

File: C:\xampp\htdocs\labexercise007\application\controllers\login.php Line: 31 Function: login

File: C:\xampp\htdocs\labexercise007\application\controllers\login.php Line: 14 Function: run

    <?php
class login_model extends CI_Model
{
  public function __construct()
  {
    parent::__construct();
    $this->load->database();
  }

  public function login($username, $password)
  {
    $condition_array = array(
      'user_name' => $username,
      'user_pass' => md5($password)
    );
    $rs = $this->db->get_where('users', $condition_array);
    $row_count = count($rs->row_array());

    if ($row_count > 0) {
      return $rs->row_array();
    } else {
      return FALSE;
    }
  }
}


    <?php
class Login extends CI_Controller
{
  public function index()
  {
    $data['title'] = 'Login';
    $this->load->view('login', $data);
  }
  public function verify()
  {
    $this->form_validation->set_rules('txtuser', 'Username', 'required');
    $this->form_validation->set_rules('txtpass', 'Password', 'required|callback_check_user');

    if ($this->form_validation->run() === TRUE) {
      if ($this->session->user_lvl == 1) {
        redirect('admin/home');
      } else {
        redirect('home');
      }
    } else {
      $this->index();
    }
  }

  public function check_user()
  {
    $username = $this->input->post('txtuser');
    $password = $this->input->post('txtpass');

    $this->load->model('login_model');
    $login = $this->login_model->login($username, $password);

    if ($login) {
      $sess_data = array(
        'account_name' => $login['user_accountname'],
        'user_lvl' => $login['user_lvl'],
        'islogged' => TRUE
      );
      $this->session->set_userdata($sess_data);
      return true;
    } else {
      $this->form_validation->set_message('check_user', 'Invalid Username/password');
      return false;
    }
  }
}


Solution 1:[1]

PHP v7.2.0 count() will now yield a warning on invalid countable types passed to the array_or_countable parameter. see more

Solution 2:[2]

If your result set is empty, the row_array() value is NULL and you cannot use count on it. Check what row_array() returns before counting.


Later edit

Try removing this block:

    if ($row_count > 0) {
      return $rs->row_array();
    } else {
      return FALSE;
    }

And replacing it with a ternary operator call:

    return $rs->row_array() ?: false;

This should solve your warning and return the proper result.

Solution 3:[3]

Did you try to get the result array? in the documentation in query builder it gives like this.

$row_count = count($rs->getResultArray()) ;

in this case it will always return an array even if you do not have a result in query.

Solution 4:[4]

Instead of counting the row array that might be null in your case so you don't have a countable object or array you should count your query results.

$row_count = count($rs->row_array()); 

Instead use the count records function built in the query builder.

  public function login($username, $password)
  {
    $condition_array = array(
      'user_name' => $username,
      'user_pass' => md5($password)
    );
    $rs = $this->db->from('users')->where($condition_array)->get();
    
    $row_count = $this->db->num_rows();

    if ($row_count > 0) {
      return $rs->row_array();
    } 
    return FALSE;
  }

Changed the function to remove the else statement because it was not needed in this case.

Also, I might add that you shouldn't be using MD5 to encrypt your passwords. MD5 is not really secure at this point.

More information on counting database results on codeigniter 3: https://codeigniter.com/userguide3/database/query_builder.html?highlight=count#limiting-or-counting-results

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivo Ivanov
Solution 2
Solution 3 John Christopher Santos
Solution 4