I need to know if the PDO extension I wrote is both syntactically and semantically. I was var_dumping () my connection variables, and although the variables are passed to the constructor (with the correct values), I cannot extract anything from my database.
I studied the PDO class in the PHP manual, and from what I found, the class I use is almost identical to the extension class specified in the examples section of the wiki page.
Here is my code:
class DBConnector extends PDO { private $host; private $username; private $password; private $db; private $dns; public function __construct($host, $username, $password, $db) { $this->host = $host; $this->username = $username; $this->password = $password; $this->db = $db; $this->dns = "mysql:dbname=".$this->db.";host=".$host; $connection = parent::__construct($this->dns, $this->username, $this->password); } }
And here is a test request that returns an array with ... nothing inside it. There is data in the database, so obviously something is wrong.
function testQuery() { global $connection; $query = " SELECT * FROM users "; $stmt = $connection->prepare($query); $result = $stmt->fetchAll(); }
Am I doing something wrong?
source share