error in mysqli code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisBull
Forum Commoner
Posts: 42
Joined: Fri Aug 20, 2010 7:43 am

error in mysqli code

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: error in mysqli code

Post 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
Post Reply