Fatal error: Call to a member function prepare() on a non-object

Hello,

Been searching for an answer on why I’m getting this error (Fatal error: Call to a member function prepare() on a non-object ) but can’t come up with anything. Just trying to learn and update a single table in a test database. I’ll post the code for both classes I’m using so maybe I can get some help with figuring out the issue. Thank you to in advance for any advice

class.database.php
[php]
class dbConnect{
protected $db_conn;
public $db_name =‘test’;
public $db_user = ‘root’;
public $db_pass= ‘’;
public $db_host=‘localhost’;

	function connect(){
		try{
						$this->$db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->$db_name",$this->db_user,$this->$db_pass);
			return $this->db_conn;
		}
		
		
		catch(PDOException $e){
			return $e->getMessage();
		}
	
	
		
		
					
	}
}

[/php]

class.manageUsers.php
[php]
include_once(‘class.database.php’);

class ManageUsers{
	public $link;
	
	function _construct(){
		$db_connection = new dbConnect();
		$this->link = $db_connection->connect();
		return $this->link;
	}
	
	function RegisterUsers($username,$password,$ip_address,$time,$date){
		$query = $this->link->prepare("INSERT INTO users (username,password,ip_address,date,time) VALUES(?,?,?,?,?)");	
		$values = array($username,$password,$ip_address,$time,$date);
		$query->execute($values);
		$counts = $query->rowCount();
		return $counts;
	}
		
		
}

$users = new ManageUsers();
echo $users->RegisterUsers('bob','bob','127.0.0.1','12:00',31-01-2012);

[/php]

Your construct should have two underscores

[php]__construct()[/php]

Wow… I feel like an idiot now… Thank you so much!

Does it work now? You know you could make this class so it’s called statically. Then there is no need to instantiate it in your classes and scripts.

Here an example:

[php]
class DB {
private static $obj;
private function __construct() {}
private function __clone() {}

public static function getInstance() {
	if (!self::$obj) {
		self::$obj = new PDO('mysql:host=www.hostname.com;dbname=database', 'user', 'pass');
		self::$obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}
	return self::$obj;
}

final public static function __callStatic($method, $args) {
	$obj = self::getInstance();
	return call_user_func_array(array($obj, $method), $args);
}

public function error($e, $file) {
	file_put_contents('/path/to/db_error.log', "[" . date('r') . "] - " . $file . " - " . $e->getMessage() . "\n", FILE_APPEND);
	exit("A database error has occured.");
}

}
[/php]

Then usage would be something like this:

[php]
try {
$sql = DB::query();
}
catch(PDOException $e) {
DB::error($e, FILE);
}
[/php]

Just wanted to throw it out there :slight_smile:

It does work now and thank you for that information. I’m trying to move forward in learning new methods and techniques on creating secure reusable bits of script and spending a lot of time reading book, reading through forums such as this one, and of course spending a lot of time on php.net

Moving forward from the basics ( usable yet not the most secure) to more advanced techniques and methods to creating bits of secure reusable code.

My major goal is to move forward into development and start creating great things that are needed by people on the web today.

Thank you again m@tt, you’re help is always appreciated.

You are definitely on the right track, keep at it :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service