致命错误:未捕获错误:调用未定义方法prepare()

时间:2017-09-21 16:48:25

标签: php prepared-statement

我是OOP的新手。这些是我的功能。我准备好的陈述不是 工作:

function __construct($dbdetails){
    $this->_dbh = Connect :: getInstance($dbdetails);
    return $this->_dbh;
}


function sendData(){        
    $stmt = $this->_dbh->prepare("INSERT INTO users (username, email, password) VALUES(:username, :email, :password)");

    $stmt->bindValue(':username', $this->_sentdetails[0]);
    $stmt->bindValue(':email', $this->_sentdetails[1]);
    $stmt->bindValue(':password', $this->_sentdetails[2]);

    $stmt->execute();
}

这个函数曾经调用过的错误是

  

致命错误:未捕获错误:调用未定义的方法Connect :: prepare();

Class Connect是我获取连接实例的地方,我很确定我没有拼写错误。准备好的陈述是在PDO课程上做的,对吧?那么,为什么我的准备声明会考虑Connect课而不是PDO

编辑:这是我的Connect课程

class Connect{

private $_dbh,
        $_host,
        $_dbname,
        $_username,
        $_password;

public static $instance;


//the actual connection
private function __construct($dbdetails){

$this->_host = $dbdetails['host'];    
$this->_dbname = $dbdetails['dbname'];    
$this->_charset = $dbdetails['charset'];    
$this->_username = $dbdetails['username'];    
$this->_password = $dbdetails['password'];

try{    
$this->_dbh = new PDO("mysql:host=$this->_host;
                            dbname=$this->_dbname;
                            charset=$this->_charset",
                            $this->_username,
                            $this->_password);  
}catch(PDOException $e){
    echo "Error : " . $e;
}
}

//creating instance for connection
public function getInstance($dbdetails){
if(!isset(self::$instance)){
     return self::$instance = new Connect($dbdetails);
}
else{
    return self::$instance->_dbh;
}
}
}

1 个答案:

答案 0 :(得分:0)

Connect::getInstance()执行不一致。当它第一次被调用时,它会创建一个new Connect对象并返回该对象。其他时候,它会返回PDO中的$this->_dbh对象。

getInstance()应始终返回Connect对象,并且您应该有另一种方法来返回PDO对象。

class Connect{

    private $_dbh,
        $_host,
        $_dbname,
        $_username,
        $_password;

    public static $instance;


    //the actual connection
    private function __construct($dbdetails){

        $this->_host = $dbdetails['host'];    
        $this->_dbname = $dbdetails['dbname'];    
        $this->_charset = $dbdetails['charset'];    
        $this->_username = $dbdetails['username'];    
        $this->_password = $dbdetails['password'];

        try{    
            $this->_dbh = new PDO("mysql:host=$this->_host;
                            dbname=$this->_dbname;
                            charset=$this->_charset",
                                  $this->_username,
                                  $this->_password);  
        }catch(PDOException $e){
            echo "Error : " . $e;
        }
    }

    //creating instance for connection
    public function getInstance($dbdetails){
        if(!isset(self::$instance)){
            self::$instance = new Connect($dbdetails);
        }
        return self::$instance;
    }

    public function getConnection() {
        return $this->getInstance()->_dbh;
    }
}

然后第一堂课应该做:

function __construct($dbdetails) {
    $this->_dbh = Connect::getInstance($dbdetails)->getConnection();
}