2014-11-02 31 views
1

我嘗試使用其他類似鏈接來回答我的問題,但是couldn不解決。其他被推薦的問題有一個PHP類,並使用它。在我的情況下,我已經有PHP類,而且我仍然得到相同的錯誤。PHP致命錯誤:調用一個成員函數prepare()在admin includes html database.class.php中的非對象45行

我做了這個類按照說明以下鏈接: culttt.com/2012/10/01/roll-your-own-pdo-php-class/

對我來說,我使用MS SQL。

以下是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(); 
} 
} 

執行從其他文件(phppdo.php)下面的PHP代碼的代碼Apache服務器如下會反映結果:

第一次嘗試執行phppdo.php將反映結果。

第二次嘗試執行phppdo.php將反映以下錯誤:

致命錯誤:調用一個成員函數準備()在C語言的非對象:\ XAMPP \ htdocs中\ sattest \管理員\包括\ html \ database.class.php on line 45

執行phppdo.php的第三次嘗試將使Apache服務器崩潰並重新啓動Apache服務。

而第四次嘗試顯示結果罰款爲第一次嘗試。爲了測試的目的,這個循環重複執行這個文件一次又一次。

// 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>"; 

以下是Apache的錯誤日誌:

[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. 

由於一噸提前對您有所幫助。 我真的需要儘快解決這個問題。

〜Satinder

+0

夥伴,該教程是愚蠢的......你爲什麼要修飾PDO並刪除新對象中的特徵/功能?構造函數正在吃掉異常。它記錄到$ this-> error(這是私人的),但沒有辦法訪問變量 – Tivie 2014-11-02 21:34:07

回答

2

好了,我要發表評論,但它得到太大......

該教程是愚蠢的。首先它包裝(裝飾)PDO類,但刪除了該類的大部分功能。另外,在構造函數中,異常被捕獲並且消息被保存到無法被訪問的私有變量。意思是你不能正確調試。

反正...

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

這意味着$dbh是不是一個對象。由於$dbh是PDO實例,因此創建所述對象時可能會出現問題(可能腳本無法連接到數據庫,由於例外情況已被禁止,因此誰知道)。

因此,請執行以下操作...在database.class中。PHP的構造函數替換此:

// 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(); 
} 

與此:

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

,然後上傳你得到的異常。例外應該是對發生的事情的自我解釋。

+0

感謝您的幫助Tivie。 – satinder 2014-11-02 22:36:02

+0

我已經放棄了該數據庫類,並直接在php文件中使用pdo代碼。工作正常。沒有錯誤。無論如何,我還是PHP和PDO的新手。你能指點我一個能找到'如何'的地方或者如何製作PDO的課程或功能嗎?只想發送查詢並將結果存儲在變量中,我可能會相應地進行處理。再次感謝! – satinder 2014-11-02 22:38:25

相關問題