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

调用未定义的方法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

图片转代码服务由CSDN问答提供 功能建议

我正在为网站编写注册脚本,并使用registration.php脚本中的函数databaseConnection测试代码 每一个工作,但现在我将databaseConnection函数替换为一个单独的文件,所以我不必每次都再次输入相同的代码。

我从databaseConnection.php中获取数据库中的实例 文件并将其加载到registration.php中的私有变量$ db_connection中,现在我想为sql语句准备连接但脚本返回错误'调用未定义的方法DatabaseConnection :: prepare()' \ n

这是databaseConnection.php脚本

 &lt;?php 
class DatabaseConnection {
 // private zodat niemand een nieuwe kan maken 
private static $ instance =  null; 
 //构造函数私有zodat niemand instantie kan klonen 
private函数__construct(){
尝试{
 $ this-&gt; db_connection = new PDO('mysql:host ='。DB_HOST。'; dbname ='  。  DB_NAME,DB_USER,DB_PASS); 
返回true; 
} catch(PDOException $ exception){
 $ this-&gt; errors [] = $ this-&gt; lang ['Database error']; 
返回false  ; 
} 
} 
公共静态函数getInstance(){
 if(self :: $ instance === null)
 self :: $ instance = new DatabaseConnection(); 
 
返回self ::  $ instance; 
} 
} 
?&gt; 
   
 
 

我在registration.php中的__construct函数中将实例加载到$ db_connection中,就像这样

  $ this-&gt; db_connection = databaseConnection :: getInstance(); 
   
 
 

现在我的代码在$上崩溃了 check_user_name = ... line

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

有人可以帮我解决这个错误吗? 我在stackoverflow上阅读了类似的问题,但无法用其中给出的信息弄清楚。

问候

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    点赞 评论
  • duanchun1881 2014-01-01 20:57

    $this->db_connection is DatabaseConnection object not a PDO object I think. Prehaps you want to extend the PDO object for example:

    class DatabaseConnection extends PDO {
    

    Otherwise you could declare wrapper functions around PDO for the methods you would like to call.

    点赞 评论

相关推荐 更多相似问题