Welcome Guest, Not a member yet? Register   Sign In
How to fix error: count(): Parameter must be an array or an object that implements Co
#1

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


Below code is my model and controller.


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

If
PHP Code:
$row_count count($rs->row_array()); 

Returns null or false you will get that error.

See what is returned:

PHP Code:
echo count($rs->row_array());
exit(); 

But your running CodeIgniter 3 which has the num_rows() method for MySQLi

PHP Code:
echo $query->num_rows();
exit();

// for testing the count
if (num_rows() > 0)
{
    //

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB