phpcodeigniteractiverecordquery-builder

"Trying to get property of non-object" error while using CodeIgniter's num_rows on query result object


When using this:

if ( $q->num_rows > 0 )

I continually get error:

Trying to get property of non-object

I'm following a guide to create a login authentication system. I'm clearly typing what the instructor has, but I cant get passed that error, and it's not just this guide, other guides I have followed, has resulted in the same error when trying to grab row data.

Here is my model:

public function verify_user($email, $password)
{
    $q = $this->db
        ->where('email_address', $email)
        ->where('password', sha1($password))
        ->limit(1)
        ->get('users');

    if ($q->num_rows > 0) {
        return $q->row();
    }
    return false;
}

Solution

  • change

    $q->num_rows
    

    to

    $q->num_rows()
    

    See Here: codeigniter guide

    Try doing:

    echo $this->db->last_query(); //after $this->db ...... get('users');
    

    to check your generated sql to see for any errors you have in your sql statement