2017-03-17 50 views
0

我已經做了各種各樣的事情來了解代碼的實際錯誤,但我無法弄清楚是什麼導致了這個錯誤。對null調用成員函數?

下面是類稱爲Topic

class Topic { 

     // Init DB variables 
     private $db; 

     /* 
     * Constructor 
     */ 
     public function __contruct() { 
      $this->db = new Database(); 

     } 

     /* 
     * Get All Topics 
     */ 
     public function getAllTopics() { 

      $this->db->query("SELECT topics.*, users.username, 
           users.avatar, categories.name 
           FROM topics 
           INNER JOIN users 
           ON topics.user_id = users.id 
           INNER JOIN categories 
           ON topics.category_id = categories.id 
           ORDER BY create_date DESC"); 


      // Assign the results 
      $results = $this->db->resultset(); 

      return $results; 
     } 
    } 

當我創建Topic類的一個對象,

$topic = new Topic(); 
$results = $topic->getAllTopics(); 

總是有這樣的錯誤:

致命錯誤:調用成員函數query()o ñ空在 /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php 上線25即$這個 - > DB->查詢(...)

Database類是可以正常使用因爲我已經爲該課程獨立運行測試,並且沒有問題。

這裏是數據庫類:

<?php 

    /* 
    * Using PDO for the first time in my life. 
    */ 

    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; 

      // Set options 
      $options = array(
       PDO::ATTR_PERSISTENT => true, 
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
      ); 

      // Create a new PDO instance 
      try { 
       $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 
      } 
      // Catch the errors using exceptions handler 
      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(isnull($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); 
     } 

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

     public function cancelTransaction(){ 
      return $this->dbh->rollBack(); 
     } 


    } 

?> 
+0

數據庫類是否在同一個文件中?如果不是,你必須「包含」/「要求」它。 –

+0

這是你完整的主題文件嗎?致命錯誤建議在第25行,在第19行上調用查詢函數。 –

+0

@TomUdding是,我包含它。 – manjula

回答

7

你有public function __contruct()一個錯字,它應該是public function __construct() {(你缺少一個或多個)

+2

我現在沒有語言來描述我的感受。我真的很慚愧,另一方面,我非常感謝你。自從過去3個小時以來,我一直堅持這一點。 – manjula

+1

別擔心,它會發生很多;) – gmc

-2

我有同樣的問題。任何問題是由於自定義的永久鏈接插件。

wp_postmeta刪除WHERE meta_key ='custom_permalink'AND meta_value LIKE'%url_against_issue%';

然後我更新

UPDATE SET wp_posts POST_NAME = 'url_against_issue' WHERE ID = 78; //注意:78是「url_against_issue」頁面的ID。

之後,問題得到解決。