dsbo44836129 2014-01-01 20:53
浏览 84
已采纳

调用未定义的方法pdo :: prepare

I'm writing a registration script for a website and tested the code with the function databaseConnection in the registration.php script, everyting worked but now I replaced the databaseConnection function to a seperate file so I dont have to type the same code again every time.

I took the instance from the database from the databaseConnection.php file and loaded it into the private variable $db_connection in registration.php, now I want to prepare the connection for a sql statement but the script returns error ' Call to undefined method DatabaseConnection::prepare()'

This is the databaseConnection.php script

<?php
class DatabaseConnection {
//private zodat niemand een nieuwe kan maken
private static $instance = null;
//constructor private zodat niemand instantie kan klonen
private function __construct(){
    try{
        $this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);
        return true;
    } catch (PDOException $exception){
            $this->errors[] = $this->lang['Database error'];
        return false;
    }
}
public static function getInstance(){
    if(self::$instance === null)
        self::$instance = new DatabaseConnection();

    return self::$instance;
}
}
?>

I loaded the instance into $db_connection in my __construct function in registration.php like this

$this->db_connection = databaseConnection::getInstance();

Now my code crashes on the $check_user_name=... line

...
        } else if ($this->db_connection != null){
        $check_username_query = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email'); 
...

Can someone help me solve this error? I read the similar questions on stackoverflow but couldn't figure it out with the information given in there.

Regards

  • 写回答

2条回答 默认 最新

  • douquan1953 2014-01-01 20:55
    关注

    Read the error message more carefully: 'Call to undefined method DatabaseConnection::prepare()'

    Your DatabaseConnection::getInstance() returns an instance of DatabaseConnection instead of PDO. One possible solution would be implementing __call() in DatabaseConnection or extending PDO like Chris Barrett said.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.