foreach循环中的PDO查询[关闭]

I would like to do something like this but it is not working, I mean no elements are returned i.e. empty query. How could i achieve that?

foreach ($ghasharr as $key=>$val)
{
$stmt = $db->prepare("SELECT COUNT(id) as ccount FROM pics WHERE gallery=:ghash");
$stmt->bindValue(":ghash", $key);
$results = $stmt->execute();
$countarr[$results['ccount']] = $results['ccount'];
}

PDOStatement::execute

Returns TRUE on success or FALSE on failure.

I.e. your $results is not an array. You want to fetch the results using one of the fetch methods of the $stmt. You also don't have to prepare the statement over and over:

$stmt = $db->prepare('SELECT COUNT(id) as ccount FROM pics WHERE gallery = :ghash');
$stmt->bindParam(':ghash', $key);

foreach (array_keys($ghasharr) as $key) {
    $stmt->execute();
    $results = $stmt->fetch(PDO::FETCH_ASSOC);
    $countarr[$results['ccount']] = $results['ccount'];
}