PHP MYSQL with commentary posts

I don’t know where to start, let me make it simple ...

I have 2 posts in tables and comments, messages consisting of two fields id, and post_nameand comments, consisting of three fields: id, commentand post_id. I have a few posts with a few comments.

How can I display all posts with related comments using a while loop?

Thanks in advance....

0
source share
4 answers
   SELECT `p`.`post_name`, `c`.`comment`
     FROM `posts` AS `p`
          JOIN `comments` AS `c` ON(`p`.`id` = `c`.`post_id`)
GROUP BY  `p`.`id`;

This will give you a result set in which each line contains a comment and the name of the message that is sent with this comment.

+2
source

, , . , .

$posts_result = mysql_query("SELECT your, columns FROM poststable");
if(mysql_num_rows($posts_result) > 0){
    $comments_result = mysql_query("SELECT comments FROM commentstable WHERE comments.post_id = $post_id");
    while($post = mysql_fetch_array($posts_result){
        // print post details

        if(mysql_num_rows($comments_result) > 0){
            while($comment = mysql_fetch_array($comments_result)) {
                // print comment details
            }
        } else {
            // print comments default for when there are no comments
        }
    } // End posts while
} else {
    // print no posts found
}
+2

select * from posts p, comments c where p.id=c.post_id group by p.id;

and iterate over the results and display them as you wish.

0
source

This should help you:

$sql = 'SELECT c.*, p.post_name FROM posts p INNER JOIN comments c ON p.id = c.post_id ORDER BY p.id DESC, c.post_id DESC';
$result = mysql_query($sql);
$previous_post = 0;
while ($data = mysql_fetch_assoc($result)) {
    if ($previous_post != $data['post_id']) {
        echo '<h1>' . $data['post_name'] . '</h1>';
        $previous_post = $data['post_id'];
    }
    echo '<p>' . $data['comment'] . '</p>';
}
-1
source

All Articles