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.
$reponse->execute();before first while. OR replace$bdd->prepare();with$bdd->query();echo $bdd->rowCount();to check numbers of rows selected