Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I'm new at php oop, and I have a question.

I have this method in my class:

PHP
public function find($user = null) {
		if ($user) {
			$field = (is_numeric($user)) ? 'id' :'username';
			$data = $this->_db->get('users', array($field, '=', $user));
			//var_dump($data);
			//die();

			if ($data->count()) {
				
				$this->_data = $data->first();
				return true;
			}
		}
		return false;
	}


When I call class function count() I got message:
Call to a member function count() on a non-object


And here is my DB class methods:


public function action($action, $table, $where = array()) {
		if (count($where) === 3) {
			$operators = array('=', '<', '>', '<=', '>=');

			$field    = $where[0];
			$operator = $where[1];
			$value    = $where[2];

			if (in_array($operator, $operators)) {
				$sql = "{$action} FROM {$table} WHERE {field} {$operator} ?";

				if (!$this->query($sql, array($value))->error()) {
					return $this;
				}
			}
		}

		return false;
	}

	public function get($table, $where) {
		return $this->action('SELECT *', $table, $where);
	}

public function results() {
		return $this->_results;
	}

	public function first() {
		return $this->_results()[0];
	}

	public function error() {
		return $this->_error;
	}

	public function count() {
		return $this->_count;
	}


What I have tried:

I understand that $data isn't an object:
var_dump($data); returns boolean false????
Posted
Updated 24-Sep-18 4:16am
v2
Comments
Leo Chapiro 24-Sep-18 9:55am    
May it be, your select returns no results?
Member 13995628 24-Sep-18 10:11am    
No it returns boolean false, and db table isn't empty :/

1 solution

Most likely your DB call is returning an empty set; and you are not checking for NULL.
If this is so; the simple solution is to throw a NULL inline into your existing IF statement:
PHP
if ( $data && $data->count()) {
    $this->_data = $data->first();
    return true;
}
 
Share this answer
 
Comments
Member 13995628 24-Sep-18 10:24am    
I've tryied that before and ok it doesn't shows me an error anymore, but nothing happens after I click login, what am I missing here :/
method for login:
public function login($username = null, $password = null) {

		$user = $this->find($username);
		
		if ($user) {
			if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
				Session::put($this->_sessionName, $this->data()->id);
				return true;
			}
		}
		return false;
	}

	public function data() {
		return $this->_data;
	}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900