am new to PHP-OOP and working with the pdo am getting the following errors
Warning: PDO::__construct() expects parameter 3 to be string, array given in C:\xampp\htdocs\ooplogin\classes\DB.php on line 12
connected
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\ooplogin\classes\DB.php on line 28
Here's my /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
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
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';
});
index.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!';
}
}
Thanks a ton in advance for your help. I really need to solve this as soon as possible. -syed