[英]PHP/MySQL - Using results of a query to use in another query
I have two tables, one with posts and another one with comments: 我有两个表,一个有帖子,另一个有评论:
posts
-----
ID
user_ID
text
date
comments
--------
ID
post_ID
user_ID
text
date
I want to display every post and for each post, I want to display the related comments. 我想显示每个帖子和每个帖子,我想显示相关的评论。 So I made two queries:
所以我提出了两个问题:
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']
}
}
But the code stops and only displays the first post without the comments. 但代码停止,只显示没有评论的第一篇文章。
Try to insert 尝试插入
$reponse->execute();
before first while
. 之前,首先
while
。 OR replace $bdd->prepare();
或者替换
$bdd->prepare();
with $bdd->query();
with
$bdd->query();
Typo error: 错字错误:
$get_comments->fecth()
check your fetch()
spelling $get_comments->fecth()
检查你的fetch()
拼写
is the select query even correct?? 选择查询是否正确?
SELECT posts.ID AS post_ID, posts.user_ID, posts.text, posts.date
ORDER BY posts.ID DESC
It has no FROM clause. 它没有FROM子句。 Should have been as below:
应该如下:
SELECT posts.ID AS post_ID, posts.user_ID, posts.text, posts.date FROM posts
ORDER BY posts.ID DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.