コードコースの 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;
}
}
}
}
これは、クラス DB の関数にクエリを渡す場所からの index.php です。
$user = DB::get_instance()->query("Select * from users");