Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:59:32+00:00 2026-06-15T07:59:32+00:00

I am trying to make a page with a list of posts, and underneath

  • 0

I am trying to make a page with a list of posts, and underneath each post all the comments belonging to that post. Initially I wanted to use just one query to retrieve all the posts + comments using the SQL’s JOIN, but I find it impossible to for example retrieve a post with multiple comments. It only displays the posts with a maximum of 1 comment per post, or it just show a post multiple times depending on the amount of comments.

In this related question, somebody talked about using 2 queries:
How to print posts and comments with only one sql query

But how do I do this?

I’ve got the query and a while loop for posts, but I obviously don’t want to run a query for comments for each post inside that loop.

$getPost = mysql_query('SELECT p.post_id,
                               p.user_id,
                               p.username,
                               p.content
                        FROM post p
                        ORDER BY p.post_id DESC');

while($row = mysql_fetch_array($getPost)) 
{ 
... 
}

Table structure (reply is the table for storing comments):

POST (post_id (primary key), user_id, username, content, timestamp)
REPLY (reply_id (primary key), post_id, username, reply_content, timestamp)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:59:33+00:00Added an answer on June 15, 2026 at 7:59 am

    You can do it in a single query, which is OK if the amount of data in your original posts is small:

    $getPost = mysql_query('SELECT
      p.*,
      r.reply_id, r.username r_username, r.reply_content, r.timestamp r_timestamp
      FROM post p
        left join reply r
      ORDER BY p.post_id DESC'
    );
    
    $posts = array();
    $last_id = 0;
    while($row = mysql_fetch_array($getPost)) 
    { 
      if ($last_id != $row['post_id']) {
        $posts[] = array(
          'post_id' => $row['post_id'],
          'user_id' => $row['user_id'],
          'username' => $row['username'],
          'content' => $row['content'],
          'timestamp' => $row['timestamp'],
          'comments' => array()
        );
      }
    
      $posts[sizeof($posts) - 1]['comments'][] = array(
        'reply_id' => $row['reply_id'],
        'username' => $row['r_username'],
        'reply_content' => $row['reply_content'],
        'timestamp' = $row['r_timestamp']
      );
    }
    

    Otherwise, break it into two queries like so:

    $getPost = mysql_query('SELECT
      p.*,
      FROM post p
      ORDER BY p.post_id DESC'
    );
    
    $rows = array();
    $ids = array();
    $index = array();
    while($row = mysql_fetch_assoc($getPost)) {
      $row['comments'] = array();
      $rows[] = $row;
      $ids[] = $row['post_id'];
      $index[$row['post_id']] = sizeof($rows) - 1;
    }
    
    $getComments = mysql_query('select r.* from replies r where r.post_id in ("'
      . join('","', $ids)
      . '")');
    while ($row = mysq_fetch_assoc($getComments)) {
      $rows[$index[$row['post_id']]]['comments'][] = $row;
    }
    

    … Or something like that. Either option allows you to litter your first query with WHERE clauses and so forth to your heart’s content. The advantage of the 2nd approach is that you don’t re-transmit your original post data for each comment!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a list view in a page that is liked form
I'm trying to make parts of a list hidden when the page loads. Specifically
So I'm trying to make a registration page that feeds back a variable when
I'm new to rails and I'm trying to make a help page that just
Ok so I am trying to make a list of links in a page
I'm trying to make an app that uses the list of songs that a
I'm trying to make a page that lists records, and allows to show a
I'm trying to make a page that will show Arabic/Hebrew in the URL. for
I am trying to make a regex that finds all names, url and phone
I'm trying to make a page that my mobile phone app can hit to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.