The issue is with these lines:
$result = $sth->fetchAll(PDO::FETCH_ASSOC);while ($row = $result) { $data[] = $row;}
You're assigning $result
, and then using that value in your while loop. What is happening is that you're then assigning $row
with the same value over and over, until you run out of memory.
To fix it, you just need to move the assignments:
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $data[] = $row;}
That will loop through the database, assigning each row into $data
.
You can also pull back everything in one go, as your code currently does, using fetchAll
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
In that case, everything is already assigned to $result
, so you don't need the while loop at all. If you have a look at the man page, you can see the format that fetchAll
returns you data - it might not be exactly what you're expecting.