数据库查询仅返回第一个匹配结果


DB query returns only first matching result

// Get all categories
$query = "SELECT name FROM bolt_taxonomy WHERE taxonomytype = :taxonomytype AND contenttype = :contenttype";
$map = array(
    ':taxonomytype'  => 'categories',
    ':contenttype' => 'news',
);
$categories = $this->app['db']->fetchAssoc($query, $map);
$response = $this->app->json(array('categories' => $categories));
return $response;

返回:

{
    "categories": {
        "name": "life"
    }
}

这只是bolt_taxonomy表上与上述条件匹配的第一个条目。如何让它返回整个类别列表?

现在可以使用fetchAll来解决:

// Get the category
    $query = "SELECT name FROM bolt_taxonomy WHERE taxonomytype = 'categories' AND contenttype = 'news'";
    $categories = $this->app['db']->query($query);
    $result = $categories->fetchAll();
    $response = $this->app->json(array('categories' => $result));
    return $response;

您需要使用 whileforeach 循环进行填充。

$categories = $this->app['db']->fetchAssoc($query, $map);
foreach($categories as $category) {
   $result[] = $category;
}
$response = $this->app->json($result);
echo $response;
return $response;