duanrenchuo9244 2014-01-05 21:34
浏览 58
已采纳

无法将bindValue()的值添加到pdo对象

I'm writing a registration script that checks if the username already exists. First I had troubles getting my database instance from databaseconnection.php to registration.php, this was the error: Call to undefined method DatabaseConnection::prepare().

But now I'm getting past the prepare() line and want to give my values to the query with bindValue(), but this next error is what I get 'Call to a member function bindValue() on a non-object'.

Does this still means I didn't get the correct database instance but a instance of the databaseconnection.php class?

This is databaseconnection.php

    <?php
class DatabaseConnection {

    private static $instance = null;
    public $db_connection;

    private function __construct(){
        try{
            $this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);
            $this->db_connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            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;
    }

    public function __call($method, $args) {
       $callable = array($this->pdo, $method);
       //is_callable, verify that the contents of a variable can be called as a function
       if(is_callable($callable)) {
           return call_user_func_array($callable, $args);
       }
   }
}

?>

In registration.php I use this code to get the instance into my local private variable:

class Registration

{
    private $db_connection = null;


        public function __construct()
    {
        $this->db_connection = databaseConnection::getInstance();
                ...
    }
...
}

And would like to be able to execute this query in registration.php:

...
        $check_username_query = $this->db_connection->prepare('SELECT user_name, user_email FROM users WHERE user_name=:user_name OR user_email=:user_email');
        $check_username_query->bindValue(':user_name', $user_name, PDO::PARAM_STR);
        $check_username_query->bindValue(':user_email', $user_email, PDO::PARAM_STR);
        $check_username_query->execute();
        $results = $check_username_query->fetchAll();
...
  • 写回答

1条回答 默认 最新

  • dongwopu8210 2014-01-05 21:51
    关注
    $callable = array($this->pdo, $method);
    

    You have instance of PDO in $this->db_connection not $this->pdo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了