我遇到的问题是我经常用于不同网站的框架。我从我拥有的工作网站复制并粘贴框架,除了我的数据库信息之外什么都没改变,但由于某种原因,我收到了与我的数据库文件相关的多个错误。我包含部分文件来显示我的数据库文件和我的init文件中的相关代码。
有人看到我做错了吗?
我收到以下行的错误:
第13行)
注意:数组到字符串的转换和警告:PDO :: __ construct()要求参数2为字符串$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
我的调试调用了这个:
DEBUG DB::query called
SQL: SELECT * FROM users WHERE username = ?
PARAMS: test
然后在第33行,
它给出了这个错误:
Fatal error: Call to a member function prepare() on a non-object in
对于这一行:if($this->_query = $this->_pdo->prepare($sql)) {
数据库类
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0,
$_errmsg = "";
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/password'));
/*$host = config::get('mysql/host');
$database = config::get('mysql/db');
$username = config::get('mysql/user');
$pasword = config::get('mysql/password');
$dbh = new PDO('mysql:host=$host;dbname=$database', $username, $pasword);*/
} 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()){
echo "DEBUG DB::query called<br>SQL: $sql<br><br>PARAMS: " . implode("<br>", $params) . "<hr>\n";
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
echo "DEBUG: prepared statement created ok<hr>\n";
$x = 1;
if(count($params)){
foreach($params as $param){
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
$this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
echo "DEBUG: query succeeded, rowcount was: " . $this->_count . "<hr>\n";
} else {
"DEBUG: query failed to execute, reason:<br>" . implode( "<br>", $this->_query->errorInfo() ) . "<hr>\n";
$this->_error = true;
}
} else {
echo "DEBUG: Failed to create prepared statement<hr>\n";
}
return $this;
}
Init class
$GLOBALS['config'] = array(
'mysqli' => array(
'host' => 'localhost',
'username' => 'actual',
'password' => 'actual',
'db' => 'actual'
),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800
),
'session' => array(
'session_name' => 'user',
'token_name' => 'token'
)
);
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';