-1

Here is my "PHP-7.1.6" code for executing some products from the database, and it is unable to execute my query() function at $this->_qry->execute() to print success Message print_r("Execution Success ") below it.

class Db{
    private static $_instance = null;
    private $_conn, $_qry, $_error = false; 

    private function __construct(){
        try{
            $this->_conn = new PDO("mysql:host=127.0.0.1;dbName=online_shop", "root", "");
            //echo "Conected to Db <br/>";
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }

    public static function getInstance(){
        if(!isset(self::$_instance)){
            return self::$_instance = new Db();
        }
        return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_qry = $this->_conn->prepare($sql)){              
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_qry->bindValue($x, $param);
                    $x++;
                }
            }
            if($this->_qry->execute(){
            foreach($this->_qry->fetchAll() as $row){
                print_r("Execution Success ");
            }}
        }
    }   
}

echo Db::getInstance()->query("SELECT name FROM products WHERE id = ?", array(1));
manoj03h
  • 37
  • 7
  • Two typos: Missing parenthesis at `if ($this->_qry->execute() {`, and missing quote at `$this->_conn = new PDO("mysql:host=127.0.0.1;dbName=online_shop, "root", "");` – Qirel Jun 27 '19 at 23:28
  • By the way, did you know you could just do `$this->_query->execute($params)` instead of binding the values individually? – Don't Panic Jun 27 '19 at 23:33
  • in " if ($this->_qry->execute() {" where should i place the parenthesis... plz reply – manoj03h Jun 27 '19 at 23:35
  • still aafter doing so `$this->_query->execute($params)` i got the same problem.. – manoj03h Jun 27 '19 at 23:43
  • Sombody please suggest me the right answer please... – manoj03h Jun 27 '19 at 23:51
  • It should be `if($this->_qry->execute())` -- a second parenthesis after `execute()` – Barmar Jun 28 '19 at 00:02
  • Possible duplicate of [My PDO Statement doesn't work](https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work) – Dharman Jun 28 '19 at 00:14
  • the problem in this `$this->_conn = new PDO("mysql:host=127.0.0.1;dbName=online_shop", "root", "");` here the bdName should be dbname instead of dbName after doing so it works fine... – manoj03h Jun 28 '19 at 00:18

1 Answers1

0

the problem in this $this->_conn = new PDO("mysql:host=127.0.0.1;dbName=online_shop", "root", ""); here the bdName should be dbname instead of dbName after doing so it works fine...

manoj03h
  • 37
  • 7