1

我有两张桌子,一张有帖子,另一张有评论:

posts
-----
ID
user_ID
text
date

comments
--------
ID
post_ID
user_ID
text
date  

我想显示每个帖子,对于每个帖子,我想显示相关评论。所以我做了两个查询:

include('bdd.php');
$reponse = $bdd->query('
    SELECT posts.ID AS post_ID, posts.user_ID, posts.text, posts.date FROM posts
    ORDER BY posts.ID DESC
');
while ($post = $reponse->fetch()){
    //displaying a post
    $get_comments=$bdd->query('SELECT * FROM comments WHERE post_ID ='.$post['post_ID']);
    while($comment = $get_comments->fecth()){
         //displaying a comment
         echo $comment['text']
    }
}

但是代码停止了,只显示没有评论的第一篇文章。

4

2 回答 2

2

尝试插入

 $reponse->execute();

在第一之前while。或替换 $bdd->prepare();$bdd->query();

打字错误:

$get_comments->fecth()检查你的fetch()拼写

于 2013-09-29T11:51:10.537 回答
1

选择查询是否正确?

SELECT posts.ID AS post_ID, posts.user_ID, posts.text, posts.date
ORDER BY posts.ID DESC

它没有 FROM 子句。应该如下:

SELECT posts.ID AS post_ID, posts.user_ID, posts.text, posts.date FROM posts
ORDER BY posts.ID DESC
于 2013-09-29T10:49:57.100 回答