pdo的PHP oop错误 - 在非对象上调用成员函数prepare()

时间:2015-01-26 05:14:19

标签: php oop pdo

我是PHP-OOP的新手,并且使用pdo会遇到以下错误

警告:PDO :: __ construct()要求参数3为字符串,数组在第12行的C:\ xampp \ htdocs \ ooplogin \ classes \ DB.php中给出

连接

致命错误:在第28行的C:\ xampp \ htdocs \ ooplogin \ classes \ DB.php中的非对象上调用成员函数prepare()

这是我的/db.php



<?php
class DB{
	private static $_instance = null;
	private $_pdo, 
			$_query, 
			$_error = false, 
			$_results, 
			$_count = 0;

	private function __construct(){
		try {
			$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname='. Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/passsword'));
			echo "connected";
		} catch (PDOException $e) {
			die($e->getMessage());
		}
	}

	public static function getInstance() {
		if (!isset(self::$_instance)) {
			self::$_instance = new DB();
		}
		return self::$_instance;
	}

	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, $params);
					$x++;
				}
			}

			if ($this->_query->execute()) {
				$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
				$this->_count = $this->_query->rowCount();
			} else {
				$this->_error = true;
			}
		}
		return $this;
	}
	public function error(){
		return $this->_error;	
	}
}
&#13;
&#13;
&#13; 的config.php

&#13;
&#13;
<?php
class Config{
	public static function get($path = null){
		if($path){
			$config=$GLOBALS['config'];
			$path = explode('/',$path);

			foreach ($path as $bit) {
				if(isset($config[$bit])){
					$config = $config[$bit];					
				}
			}
			return $config;
		}
		return false;
	}
}
&#13;
&#13;
&#13; 的init.php

&#13;
&#13;
<?php
session_start();

$GLOBALS['config'] = array(
		'mysql' =>array(
			'host' => '127.0.0.1',
			'username'=>'root',
			'password' =>'',
			'db' =>'spyroll'
		),
		'remember' =>array(
			'cookie_name' =>'hash',
			'cookie_expiry' =>604800
		),
		'session' =>array(
			'session_name' => 'user'
		)
	);

spl_autoload_register(function($class){
	require_once 'classes/'. $class . '.php';
});
&#13;
&#13;
&#13;

的index.php

&#13;
&#13;
<?php
require_once 'core/init.php';

$user = DB::getInstance()->query("SELECT username FROM users WHERE username = ?", array('alex'));

if($user->error()) {
	echo 'no user';
} else {
	echo 'OK!';
}
&#13;
&#13;
&#13;

}

提前感谢您的帮助。我真的需要尽快解决这个问题。 -syed

1 个答案:

答案 0 :(得分:0)

您的密码请求中只有一个拼写错误。尝试

$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname='. Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));

而不是

$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname='. Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/pass**s**word'));

会发生什么?如果找不到“配置路径”,则Config :: get方法将返回完整的配置数组。如果找不到密钥,也许你可以抛出一个异常...或者你返回false(如果没有给出$ path也这样做):

<?php
class Config{
    public static function get($path = null){
        if($path){
            $config=$GLOBALS['config'];
            $path = explode('/',$path);

            $result = false;
            foreach ($path as $bit) {               
                if(isset($config[$bit])){
                    $result = $config[$bit];                    
                }
            }
            return $result;
        }
        return false;
    }
}

在这种情况下,你的Config :: get方法也没有意义。它无法处理更复杂的数组,但是处理这种简单的二维数组太复杂了。也许你可以使用像Config :: get($ key,$ value)这样的方法?

<?php
class Config{
    public static function get($key, $value){
        if (isset($GLOBALS['config'][$key]) && isset($GLOBALS['config'][$key][$value]))
            return $GLOBALS['config'][$key][$value];
        else {
            trigger_error('Unknown configuration entry.', E_USER_WARNING);
            return false;
        }
    }
}