Frederick M. Rogers
Frederick M. Rogers

Reputation: 921

Catchable fatal error: Object of class PDO could not be converted to string in

I am having trouble location an error. The error i am getting is this:

Catchable fatal error: Object of class PDO could not be converted to string in postClass.inc.php on line 14

file: 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);

?>

Any and all assistance would be appreciated.

Upvotes: 0

Views: 754

Answers (1)

rafwlaz
rafwlaz

Reputation: 484

Spelling:

$this->$database = $database;

should be

$this->database = $database;

Upvotes: 2

Related Questions