2016-12-18 59 views
0

我無法定位錯誤。我得到的錯誤是這樣的:可捕捉的致命錯誤:類PDO的對象無法轉換爲字符串

開捕致命錯誤:類PDO的對象無法轉換爲字符串postClass.inc.php線14

文件:postClass.inc。 php

<?php 

include ('connect.inc.php'); 

class SimplePosts { 

    private $database; 
    private $sqlStatement; 
    private $queryColumn; 
    private $query; 
    private $result; 

    public function __construct($database, $column) { 
     $this->$database = $database; 
     $this->buildQuery($column); 
     $this->query = $this->$database->prepare($this->sqlStatement); 
     $this->query->execute(); 
    } 

    private function buildQuery($column) { 
     switch($column) { 
      case 'id': 
       $this->queryColumn = 'id'; 
       break; 
      case 'title': 
       $this->queryColumn = 'title'; 
       break; 
      case 'content': 
       $this->queryColumn = 'content'; 
       break; 
      case 'user_id': 
       $this->queryColumn = 'user_id'; 
       break; 
      default: 
       echo "'$column' is not a valid column"; 
     } 
     $this->sqlStatement = "SELECT * FROM posts ORDER BY $this->queryColumn"; 
    } 

    public function getPost() { 
     try { 
      $this->result = $this->query->fetch(PDO::FETCH_ASSOC); 
      return $this->result; 
     } catch (PDOException $e) { 
      echo "Error: " . $e->getMessage() . "<br />"; 
      echo "Unable to retrieve results <br>"; 
     }  
    } 

    public function id() { 
     echo $this->result['id']; 
    } 

    public function title() { 
     echo $this->result['title']; 
    } 

    public function content() { 
     echo $this->result['content']; 
    } 

    public function userId() { 
     echo $this->result['user_id']; 
    }  

} 

$test = new SimplePosts($database, 'id'); 

var_dump($test); 

?> 

任何和所有的援助,將不勝感激。

回答

2

拼寫:

$this->$database = $database;

應該

$this->database = $database;

+0

@rawlaz,親愛的上帝,我一直盯着自己這段代碼盲目....我怎麼能錯過..謝謝你親切的先生/女士。 –

相關問題