-1

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;
}
1
  • This question is Off-topic: Typo because the asker was trying to select from a non-existent table, readers could not have known that from the details provided, and the num_rows method call was missing its parentheses. Commented Mar 24 at 22:29

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

changed it, now this error: Fatal error: Call to a member function num_rows() on a non-object in /home/techf/public_html/application/models/admin_model.php on line 18
@bnelsonjax try doing echo $this->db->last_query(); after your get('users'); to check for any errors in your sql statement generated..
excellend idea on the last_query. i added that and commented out the num_row and it displayed correctly: SELECT * FROM (users) WHERE email_address = '[email protected]' AND password = 'a36e1f2d2c1309e9f4c6d2ef75d01dd4fd21c' LIMIT 1 so its grabing the correct query, so why is num_rows not working?
ok im stupid, i was selecting the wrong table, it was 'users' and should ahve been 'admins'. but it was thanks to your idea with the last_query as I was able to run that into myphpadmin and it wasnt getting a result which lead me to discovering it was wrong table. thansk so much!
@bnelsonjax glad your figured it out.. :)
|
2

You need to use parenthesis after num_rows, because num_rows is a function (method), not a property:

$q->num_rows()

Also, if the query does not return any result, then $q will not be an object, but a boolean FALSE.

Suggestion:

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

    return (empty($user) ? false : $user);
}

1 Comment

The function can be one-liner: return $this->db->where('email_address', $email)->where('password', sha1($password))->limit(1)->get('users')->row();
1

Possible Reasons

its a function, Not a property. Change it to $qry->num_rows()

or

Your query is not successful and returns FALSE.

Try to var_dump your result first.

try this

if( $qry != FALSE && $qry->num_rows() > 0 )

//Query is success and there is at least one row.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.