Problem with PDO when executing php

l’m having a problem with PDO.
Everytime i run the php file. i get the following error :

Fatal error : Uncaught Error: Call to a member function prepare() on null…

The php file:

 try {
                include '../connection.php';
                $database = new Database;
                $database->query('SELECT MAX(id) AS max_id FROM subscriber'); 
                $rows = $database->resultset();
                $id = $rows[0]["max_id"] + 1; // add with new ID



//defining the connection and other function that will be perform for linking with database
class Database
    private $host = '';
	private $user = '';
	private $pass ='';
	private $dbname = 'data.db';
    private $dbh; //database handling
    private $error;
    private $stmt; //statements

    public function __construct()
        // Set DSN(Data Source Name)

        $dsn = 'sqlite:'.$this->dbname;

        // Set Options
        $options = array(
            PDO::ATTR_PERSISTENT => true, 
            PDO::ATTR_EMULATE_PREPARES => true

        // Create new PDO
        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);

    public function bind($param, $value, $type = null)
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    $type = PDO::PARAM_STR;
            $this->stmt->bindValue($param, $value, $type);
//to execute prepared statements
    public function execute()
        return $this->stmt->execute();
//to select records from database
    public function resultset()
        // retrieve the resultset in the form of an associative array
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
//execute function lastInserted Id
    public function lastInsertId()

i try some solution on google but still having the error. Do i miss something ?

Thanks in advance,

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.