嘿,我正在使用 codecourse 中的 oop 练习登录系统。直到数据库连接一切都朝着正确的方向发展,但是当我尝试运行查询时,执行返回 false。
class DB{
private static $_instance = null;
private $_pdo,
$_query,
$_error=false,
$_results,
$_count=0;
private function __construct(){
try{
$this->_pdo = new PDO(
"mysql:".Config::get("mysql/host").";
dbname=".Config::get("mysql/db").";
port=".Config::get("mysql/port")."\",\""
.Config::get("mysql/username")."\",\""
.Config::get("mysql/password")."\"");
}catch(Exception $e){
echo $e->getMessage();
exit();
}
}
public static function get_instance(){
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()){ //here i m geeting value as false
echo "success";
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
}else{
echo "error";
$this->_error = true;
}
}
}
}
这是我的 index.php,我从中将查询传递给 DB 类中的函数。
$user = DB::get_instance()->query("Select * from users");