Forum Moderators: coopster
What I want is to be able to get a count of each of the options for statistical purposes. That was relatively easy, as shown from the excerpt below:
...
$sql = "SELECT response, response_comment FROM responses";
$stmt = $dbHandle->prepare($sql);
$stmt->execute();
...
$stats = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
print_r(array_count_values($stats)); //only print_r for debugging purposes
...
I had to use PDO::FETCH_COLUMN because if I use FETCH_ASSOC, it gives me a multidimensional array that array_count_values can't handle.
Later on the page I want to also loop through the comments for each option. Here's that excerpt:
...
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<p>$row[response]</p>";
echo "<p>$row[response_comment]</p>";
}
...
However, when that code comes after the stats fetch, nothing shows up. If I move the comments fetch before the stats, the stats don't show up. I assume this is because PDO has already iterated through the statement object.
In MySQL I normally just use a data seek to restart the result array, but I've found that it's impossible with PDO (5th question here: [wiki.pooteeweet.org...] )
Anyone with experience with PDO know the best way to do this? Is there a way to restart the $stmt object? I guess I could have two queries, but that seems over the top...
SELECT COUNT(*) FROM ....