0
votes

This code get an error: Fatal error: Call to a member function prepare() on a non-object in D:\ProgramFile\Xampp\htdocs\site\shop\class\DB.php on line 32

public function query($sql, $params = array()){
    $this->_error= false;
    if($this->_query = $this->_pdo->prepare($sql)){
        $x = 1;
        if(count ($params)){
            foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()){
            $this->_result = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

what can causing it. Sorry I'm very new to PDO and OOP.

EDITED my PDO declear and __construct function is

private $_pdo;      
private function __construct(){
    try {
        $this->_pdo = new PDO(
        'mysql:host=' . config::get('mysql/host'),//get host name
        'dbname=' . config::get('mysql/db'),//get database name
        config::get('mysql/username'), 
        config::get('mysql/password'));
    } catch (PDOException $e){
            die($e->getMessage());
        }
}
1
Simply means that $this->_pdo is not an object with such a function. Where is your PDO declared and what is the name of it? - Vincent Ramdhanie
You are probably missing some error handling around setting up your PDO connection. It's reasonably likely that this would occur in your __construct() function. Debugging your connection will tell you what the problem is - scrowler

1 Answers

0
votes

after $this->_pdo = new PDO(...), your $this->_pdo is false. var_dump($this->_pdo) to see it.