PHP Fatal error: Call to a member function prepare() on a non-object in admin\includes\html\database.class.php on line 45

I tried getting the answer using other similar links to my question, but couldn't solve. Other questions refereed to have a PHP class, and use it. In my case I already have PHP Class, and I still get the same error.

I made this class as per explanation on the following link: culttt.com/2012/10/01/roll-your-own-pdo-php-class/

In my case I'm using MS SQL.

Following is the code of database.class.php

class Database{ private $dbtype = DB_TYPE; 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 if ($this->dbtype == 'mysql') { $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; } if ($this->dbtype == 'sqlsrv') { $dsn = 'sqlsrv:server=' . $this->host . ';Database=' . $this->dbname; } // Set options $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); // Create a new PDO instanace try{ $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); } // Catch any errors 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_ASSOC); } public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_ASSOC); } public function rowCount(){ return $this->stmt->rowCount(); } public function lastInsertId(){ return $this->dbh->lastInsertId(); } public function beginTransaction(){ return $this->dbh->beginTransaction(); } public function endTransaction(){ return $this->dbh->commit(); } public function cancelTransaction(){ return $this->dbh->rollBack(); } public function debugDumpParams(){ return $this->stmt->debugDumpParams(); } }

Executing the following PHP code from other file (phppdo.php) will reflect the result as follows with Apache server:

First attempt executing phppdo.php will reflect the result.

Second attempt executing phppdo.php will reflect the following error:

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\sattest\admin\includes\html\database.class.php on line 45

Third attempt executing phppdo.php will crash the Apache server and restart Apache service.

And fourth attempt shows the result fine as per first attempt. This cycle repeats upon executing this file again and again for the testing purposes.

// Include database class include('database.class.php'); // Define configuration define("DB_TYPE", "sqlsrv"); // Values allowed: sqlsrv, mysql, sqlite define("DB_HOST", "localhost"); define("DB_USER", "dbuser"); define("DB_PASS", "password"); define("DB_NAME", "dbname"); $database = new Database(); $database->query('SELECT * FROM incidents WHERE incident_id = 1 '); $database->execute(); $rows = $database->resultset(); echo "<pre>"; print_r($rows); echo "</pre>";

Following are the Apache Error logs:

[Mon Nov 03 09:35:27.883145 2014] [core:warn] [pid 14276:tid 256] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run? [Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations [Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37 [Mon Nov 03 09:35:28.796237 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache' [Mon Nov 03 09:35:28.799237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 4528 [Mon Nov 03 09:35:30.490406 2014] [mpm_winnt:notice] [pid 4528:tid 268] AH00354: Child: Starting 150 worker threads. [Mon Nov 03 09:35:36.041961 2014] [:error] [pid 4528:tid 1756] [client 127.0.0.1:63411] script 'C:/xampp/htdocs/sattest/nzta-forms/admin/pdodb-tutorial.php' not found or unable to stat [Mon Nov 03 09:36:40.478404 2014] [:error] [pid 4528:tid 1716] [client 127.0.0.1:63418] PHP Fatal error: Call to a member function prepare() on a non-object in C:\\xampp\\htdocs\\sattest\\nzta-forms\\admin\\includes\\html\\database.class.php on line 45 [Mon Nov 03 09:36:44.453802 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00428: Parent: child process exited with status 3221225477 -- Restarting. [Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations [Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37 [Mon Nov 03 09:36:45.002856 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache' [Mon Nov 03 09:36:45.004857 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 14456 [Mon Nov 03 09:36:46.597016 2014] [mpm_winnt:notice] [pid 14456:tid 268] AH00354: Child: Starting 150 worker threads.

Thanks a ton in advance for your help. I really need to solve this as soon as possible.

~Satinder

-------------Problems Reply------------

Well, I was going to comment but it got too big...

That tutorial is silly. First it wraps(decorates) the PDO class but removes most of the functionalities of the class. Also, in the constructor, the exception is caught and the message saved to a private variable that has no way of being accessed. Meaning you can't debug properly.

Anyways...

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\sattest\admin\includes\html\database.class.php on line 45

This means $dbh is not an object. Since $dbh is the PDO instance, there's a problem creating said object (maybe the script could not connect to the database, who knows, since the exception is being silenced).

So, do the following... In the database.class.php constructor replace this:

// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}

with this:

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

and then post the exception you get. The exception should be self explanatory of what's happening.

Category:php Views:0 Time:2014-11-02

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.125 (s). 11 q(s)