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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:02:02+00:00 2026-05-15T10:02:02+00:00

How do I get to display the number of posts on a topic like

  • 0

How do I get to display the number of posts on a topic like a forum. I used this… (how very noobish):

function numberofposts($n)
{
    $sql = "SELECT * FROM posts
            WHERE topic_id = '" . $n . "'";

    $result = mysql_query($sql) or die(mysql_error());          
    $count = mysql_num_rows($result);           

    echo number_format($count);
}

The while loop of listing topics:

<?php

$sql = "SELECT * FROM topics ORDER BY topic_id ASC LIMIT $start, $limit";
        $result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
?>
<div class="topics">
    <div class="topic-name">
        <p><?php echo $row['topic_title']; ?></p>
    </div>
    <div class="topic-posts">
        <p><?php echo numberofposts($row['topic_id']); ?></p>
    </div>
</div>
<?php
}
?>

Although it is a bad method of doing this… All I need is to know what would be the best method, don’t just point me out to a website, do it here, because I’m trying to learn much. Okay? 😀

Thanks.

EDIT:

SQL:

Posts table:

CREATE TABLE `posts` (
  `post_id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `topic_id` mediumint(8) NOT NULL,
  `forum_id` mediumint(8) NOT NULL,
  `user_id` mediumint(8) NOT NULL,
  `post_time` varchar(100) NOT NULL,
  `post_timestamp` mediumint(20) NOT NULL,
  `post_ip` varchar(20) NOT NULL,
  `post_reported` tinyint(1) NOT NULL,
  `post_reportdesc` text NOT NULL,
  PRIMARY KEY (`post_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` VALUES(1, 1, 0, 1, '15th Junenee', 0, '', 0, '');

Topics table:

CREATE TABLE `topics` (
  `topic_id` mediumint(8) NOT NULL AUTO_INCREMENT,
  `section_name` varchar(25) NOT NULL,
  `topic_title` varchar(120) NOT NULL,
  `topic_description` char(120) NOT NULL,
  `user_id` mediumint(8) NOT NULL,
  `topic_time` varchar(100) NOT NULL,
  `topic_views` varchar(1000) NOT NULL,
  `topic_up` mediumint(11) NOT NULL,
  `topic_down` mediumint(11) NOT NULL,
  `topic_status` tinyint(1) NOT NULL,
  PRIMARY KEY (`topic_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

This should help you understand a bit more.

  • 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-15T10:02:02+00:00Added an answer on May 15, 2026 at 10:02 am

    You can use:

    "SELECT COUNT(topic_id) FROM posts WHERE topic_id = '?'"
    

    The ? is a place-holder. If you use mysql, you should use mysql_real_escape_string:

    $sql = "SELECT COUNT(topic_id)
            WHERE topic_id = '" . mysql_real_escape_string($n) . "'";
    

    If you use mysql_fetch_array, $row[0] will be the count. You can name it but it’s not necessary.

    A better option is some kind of prepared statements, such as PDOStatement or mysqli_stmt. This helps prevent SQL injection.

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

Sidebar

Related Questions

I have this piece of code I'm trying to get to display but no
I want to display total number of user who only have posts at Wordpress.
I'm trying to get ReportViewer to display data from a BindingSource (VB.Net Winforms). I
How do I get an old VC++ 6.0 MFC program to read and display
is there any way to get the ASP.NET validation summary control to just display
This page displays beautifully in firefox but i get all kinds of problems when
This is a follow up on another problem i had with getting-the-last-record-inserted-into-a-select-query I am
In my forum that I am building, I want to get information about users
I'm writing a program to read a file and display the number of lines
I would like to get a sum from a column, with and without 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.