I am trying to create a simple query library and I am using PDO to access the database.
Let's say I have the following two classes:
class FirstClass { var $dbh; function __construct($host,$dbname,$user,$pw) { $this->dbh = new PDO ("mysql:host=$host;dbname=$dbname",$user,$pw); } function use_second($foo) { return new SecondClass ($foo,$this->dbh); } } class SecondClass { function __construct($foo, $dbh) { $sth = $dbh->prepare('SELECT * FROM atable WHERE bar = :foo'); $sth = $sth->execute(array('foo'=>$foo));
Is this the right way to use the same PDO connection between classes? - Since I seem to have some problems with this, for example, if I var_dump my connection to the second class, I get:
object(PDO)#2 (0) { }
Is this really wrong?
Also, if I run a select query and then reset the $sth variable, I just get:
bool(true)
Is it because I am processing the connection incorrectly? - If so, how can I use the same connection between classes correctly?
source share