0

Hey I am practicing login system with oop from codecourse.Till the database connectivity everything moving in right direction but when i try to run query execute return 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;
       }
    }
  }
}

Here is my index.php from where i passing query in to function in class DB.

$user = DB::get_instance()->query("Select * from users");
  • You can try `$this->_pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );` and see what kind of error message PDO throws. That should help identifying the problem. – PockeTiger Jul 20 '16 at 08:05
  • Thanks I got it.. the error due to some concatenation problem – Ashish Mehra Jul 20 '16 at 09:31

0 Answers0