Page 1 of 1

error in mysqli code

Posted: Sat Aug 28, 2010 12:19 pm
by ChrisBull
Hi, i don't really understand mysqli so i have put some code together and got an error but i have no idea how to solve it. Please could someone help?

This is the code,

Code: Select all

class DBConnection
{
	private $_hostName;
	private $_userName;
	private $_userPassowrd;
	private $_dbName;
	
	private $_dbConnection;
	private $_dbQuery;
	
	public function __construct($hostName, $userName, $userPassword, $dbName) 
	{
		$this->_hostName          = $hostName;
		$this->_userName          = $userName;
		$this->_userPassword      = $userPassword;
		$this->_dbName            = $dbName;
	}
	
	public function connectToDB() 
	{
		$this->_dbConnection      = new mysqli($this->_hostName, $this->_userName, $this->_userPassword, $this->_dbName) or die();
		
		return ($this->_dbConnection) ? true : false;
	}
	
	public function logInUser($userMail, $userPassword) 
	{
		$tempResult               = $this->_dbConnection->query("SELECT * FROM userDetails WHERE userMail='$userMail' AND userPassword='$userPassword'");
		
		if ($tempResult) 
			return $tempResult->fetch_assoc();
		else
			return false;
	}	
		
}
This is the error,
Fatal error: Call to a member function query() on a non-object in /homepages/38/d283760517/htdocs/GetBooking/UserSide/scripts/php/class/DBConnection.php on line 30

Re: error in mysqli code

Posted: Sat Aug 28, 2010 7:32 pm
by Jonah Bron
Looks like that error is coming from loginUser(). PHP says that _dbConnection is not an object, but it is defined as one in connectToDB(). Are you sure you called connectToDB()?

On another note, you should actually turn this into two classes; On that holds the base functions like connectToDB, __construct, etc., and another for things like loginUser.

class DBConnection
class CoolAbstractionFunctionsForAllKindsOfStuff extends DBConnection