列出作者和所属标题

时间:2018-02-20 14:00:11

标签: php mysql

我需要选择作者(不同的),并为每个作者列出所属的标题,不超过六个月。

$items = '';
$sqla = "select distinct auth from posts order by date";
$sta = $db->prepare($sqla);
$sta -> execute();
while ($rowa = $sta->fetch()) {
    $items .= "<div class='auth'>" . mb_strtoupper($row['auth']) . "</div>\n";
    $sqlb = "select id, date, title, subtitle, tags, from posts where `auth` = '" . $rowa['auth'] . "' and `date` < date(now() - interval 6 month)";
    $stb = $db->prepare($sqlb);
    $stb -> execute(); // line 29
    while ($rowb = $stb->fetch()) {
        $items .= $rowb['title'] . '\n'; 
    }
}

echo $items;

第29行致命错误。

1 个答案:

答案 0 :(得分:1)

您可以使用单个查询

     select distinct auth,id, date, title, subtitle, tags 
     from posts 
     where  `date` < date(now() - interval 6 month)
     order by date

无论如何,你的第二个查询中有错误(标签),从

之前删除逗号
 $sqlb = "select id, date, title, subtitle, tags from posts where `auth` = '" . $rowa['auth'] . "' and `date` < date(now() - interval 6 month)";

对于作者,你应该参考$ rowa

$items .= "<div class='auth'>" . mb_strtoupper($rowa['auth']) . "</div>\n";