PHP/MySQL - 使用查询结果在另一个查询中使用


PHP/MySQL - Using results of a query to use in another query

>我有两个表格,一个有帖子,另一个有评论:

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']
    }
}

但是代码停止,只显示没有注释的第一篇文章。

尝试插入

 $reponse->execute();

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

错别字错误:

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

选择查询是否正确?

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