2013-09-29 74 views
1

我有两个表,一个与职位和另一个注释:PHP/MySQL的 - 使用一个查询的结果在其他查询中使用

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

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

+0

:或$bdd->query();

错字错误更换 $bdd->prepare();?尝试在第一次之前插入'$ reponse-> execute();'。或用'$ bdd-> query()替换'$ bdd-> prepare();'' –

+0

是的,你说得对,当我简化代码时,我忘了它,我刚刚添加它。它不能解决问题。不管怎么说,还是要谢谢你。 – Codophage

+0

我发布它作为答案,接受它。快乐编码 –

回答

2

尝试之前,首先while插入

$reponse->execute(); 

。您使用的PDO

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

+0

我加了。它不能解决问题。不管怎么说,还是要谢谢你。 – Codophage

+0

这是拼写错误。谢谢! – Codophage

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 
+0

你说得对,事实上,我简化了代码,我忘了FROM。但这不是问题。不管怎么说,还是要谢谢你。我添加了FROM。 – Codophage

相关问题