PDO: calling the undefined DB :: query () method

Trying to get into PDO, but not very fun at the moment. I am sure that it is really simple.

EDIT: What would be the best way to do this? instead of wrapping it in a class?

classes / DB.class.php:

<?php // DB.class.php class DB { protected $db_name = "PDO"; protected $db_user = "root"; protected $db_pass = "root"; protected $db_host = "localhost"; // Establish Connection to Database. public function connect() { try { $DB = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); } catch (PDOException $e) { echo $e->getMessage(); } } } ?> 

includes / global.inc.php:

 <?php require_once 'classes/DB.class.php'; // Establish Connection to Database. $db = new DB(); $db->connect(); ?> 

index.php:

 <?php require_once 'includes/global.inc.php'; $STH = $db->query("SELECT * FROM users"); echo "<pre>"; print_r($STH->fetch()); ?> 
+4
source share
4 answers

You do not have a DB property and a query() method in your DB class. Add it like this:

 class DB { protected $db_name = "PDO"; protected $db_user = "root"; protected $db_pass = "root"; protected $db_host = "localhost"; protected $DB = null; // Establish Connection to Database. public function connect() { try { $this->DB = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name."", $this->db_user, $this->db_pass); } catch(PDOException $e) { echo $e->getMessage(); } } public function query() { return $this->DB->query(); } } 

It is best to use some ORM library or an empty PDO object - this is pretty friendly.

+3
source

Try the following:

 class DB extends PDO { protected $db_name = "PDO"; protected $db_user = "root"; protected $db_pass = "root"; protected $db_host = "localhost"; public function __construct() { try { parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass); } catch (PDOException $e) { echo $e->getMessage(); } } } $db = new DB; $db->query('SELECT * FROM something'); 

In addition, I added the $this in front of your members because $db_name and such were not declared in the method scope.

If you do not want the connection to be initiated when the object is created, you can do the following:

 class DB extends PDO { protected $db_name = "PDO"; protected $db_user = "root"; protected $db_pass = "root"; protected $db_host = "localhost"; public function __construct() { // do nothing } public function connect() { try { parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass); } catch (PDOException $e) { echo $e->getMessage(); } } } $db = new DB; $db->connect(); $db->query('SELECT * FROM something'); 

Important Note: Usually when redefining methods in children, you need to specify the same method signature as the parent (or you will get an E_STRICT error). Fortunately, this does not apply to the main classes, perhaps in order to allow such overrides.

+2
source

An object created as $ db does not have a method request. There is probably no need to wrap the PDO object inside another object, but if you do, you need to make sure that all methods are available.

+1
source

your DB class does not have a method request! you can do it like this:

 <?php // DB.class.php class DB { protected $db_name = "PDO"; protected $db_user = "root"; protected $db_pass = "root"; protected $db_host = "localhost"; private $_db; // Establish Connection to Database. public function connect() { try { $this->_db = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name, $this->db_user, $this->db_pass); } catch (PDOException $e) { echo $e->getMessage(); } } public function __call($name, array $arguments) { if(method_exists($this->_db, $name)){ try{ return call_user_func_array(array(&$this->_db, $name), $arguments); } catch(Exception $e){ throw new Exception('Database Error: "'.$name.'" does not exists'); } } } } ?> 

With the __call() magic function, you can pass all the functions supported by PDO to your newly created PDO.

+1
source

All Articles