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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:50:35+00:00 2026-05-31T00:50:35+00:00

I’ve recently began a comments/replies section for an app. My initial approach has been

  • 0

I’ve recently began a comments/replies section for an app.
My initial approach has been to query the database for the comments linked to a particular item as below:-

$commentquery = "SELECT projects_comments.*, users.user_url, users.display_name
                   FROM ".$wpdb->prefix."projects_comments projects_comments
              LEFT JOIN ".$wpdb->prefix."users users on users.ID=projects_comments.userid
                  WHERE projectid = '$projectid'
               ORDER BY projects_comments.commentid DESC";  

I then began a foreach loop with the following query inside in order to retrieve the replies:

if($comments) {
  foreach ( $comments as $c ) { 
    $replyquery = "SELECT project_replies.*, users.user_url, users.display_name
                     FROM ".$wpdb->prefix."project_replies project_replies
                LEFT JOIN ".$wpdb->prefix."users users on users.ID=project_replies.uid
                    WHERE project_replies.cid = '$c->commentid' 
                 ORDER BY project_replies.id DESC"; 
  }
}

In practice this is all working fine, however it strikes me that this is terribly inefficient and that there must be a way to retrieve all the data in one query. My concern is that with 100 comments on a page I will be performing 100 queries for the comments alone.

What is the best method to tackle this and how would I go about combining these queries to produce one large object/array with which to pull the data from.

Or alternatively, how can I at least prevent the ‘query-within-the-loop’.

  • 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-31T00:50:36+00:00Added an answer on May 31, 2026 at 12:50 am
    SELECT c.*, cu.user_url AS cu_url, cu.display_name AS cu_name,
           r.*, ru.user_url AS ru_url, ru.display_name AS ru_name
      FROM projects_comments AS c
      LEFT JOIN users        AS cu ON cu.ID = c.userid
      JOIN project_replies   AS r  ON r.cid = c.commentid 
      LEFT JOIN users        AS ru ON ru.ID = r.uid
     WHERE c.projectid = $projectid
     ORDER BY c.commentid DESC, r.id DESC
    

    The only detail to worry about is the JOIN instead of possibly using LEFT JOIN when connecting Project_Replies and Project_Comments. This will only pick up comments that have at least one reply; using a LEFT JOIN, you’ll get comments that have no replies too.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build
I have a reasonable size flat file database of text documents mostly saved in

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.