Fatal error: Uncaught Error: Call to a member function prepare() on line 31

<?php
class Database{
	private $host = DB_HOST;
	private $user = DB_USER;
	private $pass = DB_PASS;
	private $dbname = DB_NAME;

	private $dbh;
	private $error;
	private $stmt;

	public function __construct(){
		//set DSN
		$dsn = 'mysql:host=' .$this->host .';dbname='. $this->dbname.';charset=utf8mb4';
		
		//set options
		$options = array(
			PDO::ATTR_PERSISTENT => true,
			PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
		);		

		// PDO INSTANCE
		try{
			$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
		}catch(PDOException $e){
			$this->error = $e->getMessage();
		}
	}

	public function query($query){
			$this->stmt = $this->dbh->prepare($query);
	}

	public function bind($param,$value, $type = null){
			if (is_null($type)){
				switch (true) {
					case is_int($value):
						$type =PDO::PARAM_INT;
						break;
					case is_bool($value):
						$type =PDO::PARAM_BOOL;
						break;
					case is_null($value):
						$type =PDO::PARAM_NULL;
						break;					
					default:
						$type = PDO::PARAM_STR;
				}
			}
		$this->stmt->bindValue($param, $value, $type);
	}

	public function execute(){
		return $this->stmt->execute();
	}

	public function resultSet(){
		$this->execute();
		return $this->stmt->fetchAll(PDO::FETCH_OBJ);
	}

	public function single(){
		$this->execute();
		return $this->stmt->fetch(PDO::FETCH_OBJ);
	}
}

MY ERROR IS POINTING TO THE CODE BLOCK BELOW

public function query($query){
$this->stmt = $this->dbh->prepare($query);
}

i bet the rest of the error message is “… on non object” so, yeah, dbh is no object, so it has no methods, you have to put an object in there.

Most likely your PDO object is failing to instantiate. In the __construct method, temporarily replace:

try{
    $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}catch(PDOException $e){
    $this->error = $e->getMessage();
}

with:

$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);

basically just removing the try ... catch wrapper. This will print out the exception being thrown, rather than hiding it in $this->error.

1 Like

Thanks, @skawid but some other errors arose immediately i changed that

Notice : Undefined index: templates in C:\xampp\htdocs\joblister\lib\Template.php on line 13

Warning : include(): Filename cannot be empty in C:\xampp\htdocs\joblister\lib\Template.php on line 25

Warning : include(): Failed opening ‘’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\joblister\lib\Template.php on line 25

THE ERRORS ARE POINTING TO THIS LINES OF CODE:##

<?php class Template{
	//path to Template
	protected $template;
	//vars passed in
	protected $vars = array();
	
	//construct
	public function __construct($template){
		$this->template = $template;
	}

	public function __get($key){
		return $this->vars[$key];
	}

	public function __set($key, $value){
		$this->vars[$key] = $value;
	}

	public function __toString(){
		extract($this->vars);
		chdir(dirname($this->template));
		ob_start();

		include basename($this->template);
 
		return ob_get_cle[](http://)an();
	}

}

Thanks so much am a beginner in PHP

@Vaquita, please don’t start multiple threads for the same problem. I have merged the two (database) threads. You can use bbocde [code][/code] tags or markdown (three ``` back-ticks) around your code when posting it.

Next, I have seen this same/similar db class being posted recently on help forums. The only useful thing it is doing is setting the error mode to exceptions. Everything else it is doing, such as catching the connection error, but not doing anything with that information, is a waste of typing. This class does nothing to make writing code easier. Wherever you learnt or saw this coding, forget it.

The PDO extension is well written. It doesn’t need a ‘wrapper’ class added to it. Just use it directly in your code. However, in addition to setting the error mode to exceptions, you should also set emulated papered queries to false, and set the default fetch mode to whatever you want (so that you don’t need to keep specifying it in each fetch statement.)

I agree with @phdr. The class is not good. I recently uploaded a “clean PDO” example to my Github you can use as a starting point. As is, you can run any query with it with minimal code. Unless you need to get into dynamic queries, this should be all you need.

https://github.com/benanamen/clean-pdo

1 Like

Thanks so much for your time and for sharing your experience

Sponsor our Newsletter | Privacy Policy | Terms of Service