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

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

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

I made use of this to question the 2 tables:

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

Formerly, I had been only exhibiting the articles table by using this:

while($row = mysqli_fetch_array($query)) {

echo "
<div id='article'> 
         <h4>Posted on ".$row['publicationDate']."</h4>

This shows all articles (with date, title, content, etc.). Now you will find comments. How do you edit the php code (or maybe my totally incorrect, crafting the query), to ensure that it shows all articles and all sorts of comments per article as with:

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

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

An alternate is always to split the query into two.

The very first will bring back the articles you would like...

SELECT * FROM article;

After you have individuals, you will get all of the IDs and employ something similar to the next

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

This limits the MySQL queries to two although getting all of the data you'll need. Following this loop round the article data, and for the reason that loop, loop round the comments data.

This implies that, unlike using GROUP_CONCAT, additionally, you will have author data to make use of.

It isn't a really eloquent solution, but should work.

Use something similar to

SELECT a.article
       ,GROUP_CONCAT(CONCAT('<p>', c.comment, '</p>') SEPARATOR "\n") as comments
article a
INNER JOIN comment c ON (c.article_id =
WHERE = '12454';

You might want to fiddle a little using the separator.


Do note however:

It makes sense cut down towards the maximum length that's distributed by the audience_concat_max_len system variable, with a default worth of 1024. The worthiness could be set greater, even though effective maximum entire return value is restricted by the need for max_permitted_packet. The syntax to alter the need for group_concat_max_len at runtime is the following, where val is definitely an unsigned integer:

SET [GLOBAL | SESSION] group_concat_max_len = val;

See here how you can change max_permitted_packet


    SELECT, c.comment, 
  article_id, a.title, a.publicationDate, a.content
      FROM comments c
RIGHT JOIN articles a
        ON =


$lastArticleId  = 0;
$isNewArticle   = false;
while($row = mysqli_fetch_array($query)) {
    $isNewArticle = ($lastArticleId != $row['article_id']) ? true : false;
    if($isNewArticle) {
        $lastArticleId = $row['article_id']; ?>
        <div class="article">
                    <h2><?php echo $row['title']; ?></h2>
                    <h4>Posted on <?php echo $row['publicationDate']; ?></h4>
            <p><?php echo $row['content']; ?></p>
    if($row['comment'] != '') { ?>
        <p><strong><?php echo $row['author']; ?></strong> - <?php echo $row['comment']; ?></p>
    } ?>
} ?>

Consider MySQL GROUP_CONCAT that will return a comma delimited listing of products. After that you can explode that for the comments section.