我正在尝试查询我的数据库,但一直收到致命的错误消息。
致命错误:未捕获异常'PDOException',消息'SQLSTATE [3D000]:无效的目录名称:1046未选择数据库'在G:\ xampp \ htdocs \ login& register \ New_folder \ classes \ connect.php:78 Stack跟踪:#0 G:\ xampp \ htdocs \ login& register \ New_folder \ classes \ connect.php(78):PDOStatement-> execute()#1 G:\ xampp \ htdocs \ login& register \ New_folder \ core \ init.php(14):db-> query('SELECT`usernam ...',Array)#2 G:\ xampp \ htdocs \ login& register \ New_folder \ login.php(3):require('G :\ xampp \ htdocs ...')在第78行的G:\ xampp \ htdocs \login®ister\ New_folder \ classes \ connect.php中抛出#3 {main}
有人可以请求帮助,因为我已经阅读了大量有关此问题的解决方案,但似乎都没有。
CONNECT.PHP
class db{
private
$_host = 'localhost',
$_dbname = 'mydb',
$_username = 'root',
$_password = 'adoloveado',
$_pdo = NULL,
$_query,
$_count = 0,
$_error = true;
private static $_instance = NULL;
private function __construct(){
try{
$this->_pdo = new PDO("mysql:host = {$this->_host}; dbname = {$this->_dbname}", $this->_username, $this->_password);
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
if($this->_pdo){
echo "Connected to ". strtoupper($this->_dbname) ." database successfully ...<br/>";
}
}catch(PDOException $e){
die($this->_error ? $e->getMessage() : " ");
}
}
public 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, $param);
$x++;
}
}
if($this->_query->execute()){
echo "Success";
}
}
}
}
的init.php
<?php
require('classes/connect.php');
db::getInstance()->query("SELECT username FROM users WHERE username = ? OR username = ?", array(
'pingo','billy'
));