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

时间:2014-12-23 12:55:12

标签: php silex bolt-cms

// 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表上的上述条件匹配的第一个条目。如何让它返回整个类别列表?

2 个答案:

答案 0 :(得分:1)

现在使用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;

答案 1 :(得分:0)

您需要使用whileforeach循环填充。

$categories = $this->app['db']->fetchAssoc($query, $map);

foreach($categories as $category) {
   $result[] = $category;
}
$response = $this->app->json($result);
echo $response;
return $response;
相关问题