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 7955809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:48:12+00:00 2026-06-04T03:48:12+00:00

I have a following problem. I have three tables: message, comment and user. I

  • 0

I have a following problem. I have three tables: message, comment and user. I would like PHP to print the data of following fields from MySQL database as a table on my web-page: message.subject, user.username and then number of comments. Everything else works fine but I haven’t managed to get PHP to print the number of comments.

This is what I have tried to do so far:

<?php
 include("info.php");
 $connect;
 $sql="SELECT * FROM message, user
       WHERE message.userID = user.userID AND 
       ORDER BY message.messageID DESC";
 $result=mysql_query($sql) or die(mysql_error());

 $sql2="SELECT message.messageID, COUNT(*) as comments FROM comment 
        INNER JOIN
        message ON comment.messageID = message.messageID 
        GROUP BY comment.messageID";
 $result2=mysql_query($sql2) or die(mysql_error());

<table>
 <thead>
  <tr>
   <th>Subject</th>   
   <th>Sender</th>
   <th>Number of comments</th>
  </tr>
 </thead>
 <tbody>
  <?php
   while($rows=mysql_fetch_array($result)){
  ?>
  <tr>
   <td>
    <a href="php/message.php?id=<?php echo $rows['messageID']; ?>">
    <?php echo $rows['subject']; ?>
   </td>
   <td>
    <?php echo $rows['username']; ?>
   </td>                        
   <td>
    <?php while($rows2=mysql_fetch_array($result2)){ echo $rows2['comments'];}?>
   </td>
  </tr>
 <?php
 } mysql_close(); ?>
 </tbody>
</table>
  • 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-04T03:48:13+00:00Added an answer on June 4, 2026 at 3:48 am

    Couldn’t you just get all this info with one query?

    SELECT
        m.messageID,
        m.subject,
        u.username,
        c.numOfComments
    FROM
        message m
        INNER JOIN user u ON m.userID = u.userID
        LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
    ORDER BY
        m.messageID DESC
    

    Try this script:

    <?php
        include("info.php");
        connect();
        $sql = "
            SELECT
                m.messageID,
                m.subject,
                u.username,
                c.numOfComments
            FROM
                message m
                INNER JOIN user u ON m.userID = u.userID
                LEFT JOIN (SELECT COUNT(1) AS numOfComments, messageID FROM comments GROUP BY messageID) c ON m.messageID = c.messageID
            ORDER BY
                m.messageID DESC
        ";
        $result = mysql_query($sql) or die(mysql_error());
    
        echo "<table>
        <thead>
            <tr>
                <th>Subject</th>
                <th>Sender</th>
                <th>Number of comments</th>
            </tr>
        </thead>
        <tbody>\n";
        while ($row = mysql_fetch_assoc($result))
        {
            $row = array_map("htmlspecialchars", $row); // sanitize to prevent XSS
    
            echo "      <tr>
                <td><a href=\"php/message.php?id={$row["messageID"]}\">{$row["subject"]}</a></td>
                <td>{$row["username"]}</td>
                <td>{$row["numOfComments"]}</td>
            </tr>\n";
        }
        echo "  </tbody>
    </table>";
    ?>
    

    There are a few other corrections as well. For instance, your link doesn’t have a closing </a> tag, and your script may be vulnerable to XSS attacks. And then there’s the nested while-loop, which was causing unnecessary complication and bugs.

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

Sidebar

Related Questions

We have three tables that we would like to query to find out the
I have the following problem. I have three classes, A, B and C. A
I have the following problem: I got a view with two text fields and
I have three tables from a dataset ds. var test0 = from a in
I'm having a strange problem with php PDO and mysql. I have the following
I have the following tables in Core Data: Cinema (movies) Movie (title, times) DateTime
I have the following tables: CATEGORIES -- entry_id, cat_id TITLES -- entry_id, title DATA
I have three tables: 1. User (id, name, birthdate) - PRIMARY KEY ('id') 2.
I have following problem. I have created a tab based Application with three Views
I have the following problem: I am given a tree with N apples, for

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.