我是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;
}
}
配置.php
<?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;
}
}
初始化.php
<?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';
});
索引.php
<?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!';
}
}
提前感谢您的帮助。我真的需要尽快解决这个问题。-赛义德
您的密码请求中只有一个拼写错误。尝试
$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;
}
}
}