php-mvc icon indicating copy to clipboard operation
php-mvc copied to clipboard

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\joblister\lib\Database.php:32 Stack trace: #0 C:\xampp\htdocs\joblister\lib\job.php(10): Database->query('SELECT jobs.*, ...') #1 C:\xampp\htdocs\joblister\index.php(15): Job->getAllJobs() #2 {main} thrown in C:\xampp\htdocs\joblister\lib\Database.php on line 32

Open codesmith445 opened this issue 2 years ago • 1 comments

host .';dbname='. $this->dbname; // 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() { return $this->stmt->fetch(PDO::FETCH_OBJ); } }

codesmith445 avatar Feb 17 '23 10:02 codesmith445

The error message is saying $this->dbh is null - this happens when it hasn't connected for some reason, probably because of incorrect database credentials (user, password)

daveh avatar Feb 19 '23 18:02 daveh