2014-04-28 34 views
0

到處找一個答案,爲什麼我的PDO準備()函數是給我這個錯誤:PHP的成員函數致命備調用()對非目標

PHP致命錯誤:調用一個成員函數準備()在/var/www/database.class.php 26行上的非對象

我一直在尋找關於此問題的每一篇文章,但他們似乎都沒有幫助,甚至只是清除了這個錯誤。我開始在這裏:http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/ 一個還沒有改變任何東西只是試圖用POST數據使用

這是我database.class.php:

<?php 
class Database { 
    private $host = "localhost"; 
    private $user = "nicholas"; 
    private $pass = "12345"; 
    private $dbname = "sstest"; 

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

    public function __construct() { 
     $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
     $options = array(
      PDO::ATTR_PERSISTENT => true, 
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
      ); 
     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); //this is line 26 
    } 

    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_NULL; 
       break; 
      default: 
       $type = PDO::PARAM_STR; 
       break; 
      } 
     } 
     $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(); 
    } 


} 

?> 

,這裏是用我的數據庫中的PHP文件:

<?php 
include 'database.class.php'; 

$id = $_POST["id"]; 
$fname = $_POST["fname"]; 
$lname = $_POST["lname"]; 

$database = new Database(); 

$sql = 'INSERT INTO sstest (id, fname, lname) VALUES (:id, :fname, :lname)'; 

$database->query($sql); 

$database->bind(':id', $id); 
$database->bind(':fname', $fname); 
$database->bind(':lname', $lname); 

$database->execute(); 

echo $database->lastInsertId(); 
?> 

我知道java代碼發送POST數據,但我很困惑爲什麼prepare()函數說$ sql是一個非對象。任何幫助非常感謝,我一直在爲此工作2天,並且無法超越prepare()語句。

+0

*「我知道的Java代碼發送POST數據」 * ---什麼java? –

+5

那麼你是抓住你的pdo錯誤,然後什麼也不做。檢查$ this->錯誤 – Steve

+0

數據庫連接簡單地失敗,對象沒有實例化,如果你對* *錯誤*對象有方法,它會是'未定義的對象....'的函數,但它是'非object'。 – DanFromGermany

回答

1

我覺得你的問題是在這裏:

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

加入這一行的行後趕上你那裏:

var_dump($this->error);exit(); 
+0

謝謝!我能夠看到是什麼導致pdo異常,我只在以前看我的apache錯誤日誌.....但這是由於misreferenced數據庫,再次感謝您的答案....將在一分鐘內接受 –

相關問題