I've created a basic class called Content
for fetching content from a MySQL database, with a constructor like so:
$this->handle = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
and some methods utilizing this handle, chiefly one that fetches data and puts it into an array:
function select($query) {
$sth = $this->handle->prepare($query);
$sth->execute();
$result = $sth->fetchAll();
$sth->closeCursor();
return $result;
}
Nothing fancy, really. And it works terrific. Except, after a dozen or so various queries, fetchAll
all of a sudden returns an empty result. When I run the same query directly on the database, I do get a result. Also, when I run the query before running the other ones, I get a result as well. Even more oddly, the fetchAll
at this point in my script used to work fine, but when the queries that preceded it got more numerous, it started returning empty results.
Based on these findings, I assumed the scripting and database where just fine, and it most likely had something to do with PDO. Eventually, I 'fixed' it by creating a second object from my Content
class in order to fetch the data I was missing:
$contentTwo = new Content();
Of course, this should not be necessary! I'm looking for a real solution, and also an understanding as to what's going on. I've turned on all sorts of error reporting (PHP, PDO, MySQL) but there are no errors. Could it be some internal PDO mixup? Is there a maximum amount of prepared statements? Any ideas/solutions would be appreciated.