Here's a simple piece of code I'm writing:
class LessonsLearntServiceTyped
{
private $username = "***********";
private $password = "***********";
private $dsn = "mysql:dbname=lessonslearnt;host=****
******.db.******.hostedresource.com";
private $dbh;
public function __construct(){
try{
$dbh = new PDO($this->dsn, $this->username, $this->password);
}catch(PDOException $e){
echo "Connection failed: ".$e->getMessage();
}
}
public function returnAllEntries(){
$query = "select * from lessons_learnt order by id";
}
}
OK. I've not had much experience with PDO before but I've seen some examples on the net and apparently you can't serialize or de-serialize a PDO object.(that's the error message I get when I try to set a PDO object to an instance variable so that I can use it later)
Well, if we can't have PDO objects as instance variables, how then will I proceed on to writing the returnAllEntries()
method? Will I have 开发者_Python百科to create the PDO object again so I can use it there??(and also a new PDO object for each function I write that deals with the database)?
What am I missing here?
Thank you all in advance
EDIT
$this->dbh = new PDO($this->dsn, $this->username, $this->password);
is what I wanted to do.
But when I do that I get the following error message:note this error message comes from the PHPUnit console which I'm using to tunit-test this class. In the browser it doesn't show any errors.
""You cannot serialize or unserialize PDO instances"
It looks like this is a very very weird edge case with exceptions from reading this:
http://fabien.potencier.org/article/9/php-serialization-stack-traces-and-exceptions
Try taking out the $e->getMessage()
line bit in your catch statement and see if it changes anything.
I think the problem was with my instance of PHPUnit test runner.
精彩评论