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

  • Home
  • SEARCH
  • 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 6545811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:37:52+00:00 2026-05-25T11:37:52+00:00

So I have two tables, article and comments (which has one-to-many relationship (1 article

  • 0

So I have two tables, article and comments (which has one-to-many relationship (1 article – many comments)). This is how the table is structured:

Articles – id (prikey), title, publicationDate, content

Comments – com_id (prikey), author, comment, id (foreign key)

I used this to query the two tables:

SELECT * FROM articles as a INNER JOIN comments as c ON a.id = c.id

Previously, I was only displaying the articles table using this:

<?php 
while($row = mysqli_fetch_array($query)) {

echo "
<div id='article'> 
<header>
    <hgroup>
         <h2>".$row['title']."</h2>
         <h4>Posted on ".$row['publicationDate']."</h4>
    </hgroup>
</header><p>".$row['content']."</p></div>";
}
?>

This displays all articles (with date, title, content, etc.). Now there are comments. How do I edit the php code (or if my query is incorrect, how to write the query), so that it shows all articles and all comments per article as in:

Article One
 -- Comment 1
 -- Comment 2, etc.

Article Two
 -- Comment 1
 -- Comment 2, etc.
  • 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-05-25T11:37:53+00:00Added an answer on May 25, 2026 at 11:37 am

    An alternative would be to split the query into two.

    The first would bring back the articles you want…

    SELECT * FROM article;
    

    Once you have those, you can get all the IDs and use something like the following

    SELECT * FROM comments WHERE article_id IN (".$list.");
    

    This restricts the MySQL queries to 2 whilst getting all the data you need. After this loop around the article data, and in that loop, loop around the comments data.

    This also means that, unlike using GROUP_CONCAT, you will also have author data to use.

    It’s not a very eloquent solution, but should work.

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

Sidebar

Related Questions

I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I have two tables, like this: #Articles: ID | Title 1 Article title 2
I have two tables. One is article and the other structure. And the articles
I have two tables. One of events and one of articles. Each event has
I have two tables that are joined together. A has many B Normally you
I have two tables -- an article table and a vote table. Users can
I have two tables Say Table-A and Table-B Table-A id | Article Name |date
I have two tables, one for articles/polls, and one for the votes on these
suppose i have two tables. articles and comments. when i am selecting columns from
I have two mysql tables, one listing article names, the other listing the authors

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.