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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:52:27+00:00 2026-05-22T02:52:27+00:00

I have a section in my site where I do need to count the

  • 0

I have a section in my site where I do need to count the number of comments for a specific post.I actually already built a function for retrieving all the data in the comments DB table (function getComments).

the comments DB table structure is : id(PK) | authorId | datetime | postId | text

Now, since I just need to count them (the comments),I was wondering if,in term of server resources/redundancy is better to use :

$comments=new Comment();
$comments->getComments();
echo count($comments);

Or I’m better build another function (apart from ‘getComments’)like :

function countComments($postid)
{
  //count comments per post
}

thanks

Luca

  • 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-22T02:52:28+00:00Added an answer on May 22, 2026 at 2:52 am

    Counting your comments in the database is more efficient, because

    1. Only the count is transferred, so you save bandwidth between your database and the php
      process.
    2. You save IO because the result isn’t generated and the data isn’t read from disk
    3. A RDBMS is optimized for such functions.

    Assuming, that you use Zend_Db something like this should work:

    $query = $db->select()->from('comments', 'count(*)');
    $count = $db->fetchOne($query);
    

    Code redundancy isn’t a problem IMHO here, it’s just one or two lines.

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

Sidebar

Related Questions

I have a web site that built on Visual studio 2008 and i need
I have a section of my site where users can add addresses to their
This is what I have typed in the footer message section from the site
I have a Site.Master in my ASP.NET project which defines a HEAD section as
I have an ASP.NET 3.5 e-commerce site that has an admin section. I want
I have an html site head section of that html site <head> <script type=text/javascript
I need to add a user uploaded video section to my site. There'll plenty
I am currently developing a site and have a need for a javascript-based carousel/slider
I have a section of my site where I use check boxes to show
I have a few links on my site that will need to show a

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.