-2

I have the below code which, when run, returns this error:

PHP Fatal error: Call to a member function count() on a non-object in /classes/User.php on line 36

public function find($user = null) {
    if($user) {
        $field = (is_numeric($user)) ? 'id' : 'email';
        $data = $this->_db->get('*', 'users', array($field, '=', $user));
        if($data->count()) { // This is line 36, the $_db variable is an instance of the DB class
            $this->_data = $data->first();
            return true;
        }
    }
    return false;
}

What am I missing that is making $data not refer to an object?

user1710563
  • 387
  • 2
  • 7
  • 18
  • The error message does not pertain the `$_db` variable. It's about `$data`. Look up the documentation on your `->_db->get()` method to find out if it ought to return an object or not. – mario Nov 16 '13 at 03:37

1 Answers1

0

It's pretty simple, though the solution may not be: $this->_db->get is not returning an object. Depending on what _db is - is it an abstraction layer? Some crazy homebrew thing? Just looking at the syntax:

$this->_db->get('*', 'users', array($field, '=', $user));

I get a little weirded out - assuming that it's describing how keys are separated from search values, I've never seen a DB layer like that (and how often does SQL change what = does, anyway?) I'm willing to bet your database layer needs some attention, but that might be editorializing a bit. ;)

Chances are whatever it is, it's returning an error - it's (unfortunately) fairly commonplace for an object library to return FALSE instead of an object with an error code. It could even be returning an error string. Echo that bad boy and see what you get, and then fix whatever's causing it.

Winfield Trail
  • 5,535
  • 2
  • 27
  • 43