6

I need to know if PDO extension I wrote is valid both syntactically and semantically. I've been var_dumping() my connection variables and while the variables are being passed to the constructor (with correct values), I'm not able to actually fetch anything from my database.

I've researched the PDO class on the PHP manual and from what I've uncovered the class I'm using is nearly identical to the extension class given in the examples section of the wiki page.

Here's 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's a test query which returns an array with...nothing inside of it. There is data within the database, so obviously something isn't correct.

function testQuery() { global $connection; $query = " SELECT * FROM users "; $stmt = $connection->prepare($query); $result = $stmt->fetchAll(); } 

Am I doing something wrong?

2 Answers 2

11

Try this:

class DBConnector extends PDO { private $connection; 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; $this->connection = parent::__construct($this->dns, $this->username, $this->password); $this->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } public function testQuery() { $query = "SELECT * FROM a"; $stmt = $this->prepare($query); if($stmt->execute()){ return $stmt->fetchAll(); } return array(); } } $tg = new DBConnector('localhost', 'root', '', 'test'); $t = $tg->testQuery(); print_r($t); 

the $connection is local to the DBConnector::__construct and I don't see any global there. So it will not exist in your testQuery function. By moving your function to the class, and create a connection property, it is easy to use it.

Sign up to request clarification or add additional context in comments.

7 Comments

I tried that, and unfortunately my connection returns NULL when I throw a var_dump() on it. Is there anything else I can try?
Yes weird, I get the same. I updated my code. I am researching why it does that.
ok I found it, you don't need to use ->connection->prepare since you already extends it. I updated my code
Still not working :/. This is my output: object(PDOStatement)#2 (1) { ["queryString"]=> string(28) " SELECT * FROM users " } NULL Anything else? I appreciate your help, btw. EDIT: do you think something may be wrong with my port config, or a socket or something? I'm running linux, if that helps.
I just updated it, copy my code again to a php and try ( you might have to change the credentials, tables and DB). sorry about that. I get an array from my test "Array ( [0] => Array ( [part_no] => 240-63100-00 [0] => 240-63100-00 [description] => YB MV Cherry [1] => YB MV Cherry ...."
|
3

You need to execute query.

function testQuery() { global $connection; $query = " SELECT * FROM users "; $stmt = $connection->prepare($query); if($stmt->execute()){ $result = $stmt->fetchAll(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.