I'm having problems trying to query the total number of topics in each of my forums.

Basically, I am getting the same number for each forum. I only have 2 forums in the database. For testing purposes, I have 1 topic in one and 0 in the other but I am getting 1 for each forum.

Here is my query:

$query_topics = "SELECT topic_id, count(*) FROM topics JOIN forums ON (topics.topic_forum = forums.forum_id) GROUP BY topic_id";
$topics = mysql_query ($query_topics) OR die ('Cannot execute the query.');
$row_topics = mysql_fetch_assoc ($topics);
$total_topics = mysql_num_rows ($topics);

Here is my PHP for the table:

<table width="725" border="0" cellspacing="1" cellpadding="6" bgcolor="#CCCCCC">
 <tr bgcolor="#E5E5E5">
  <td width="525"><strong>Forum</strong></td>
  <td width="100"><div align="center"><strong>Topics</strong></div></td>
  <td width="100"><div align="center"><strong>Posts</strong></div></td>
 </tr>
 <?php do { ?>
 <tr bgcolor="#FFFFFF">
   <td width="525"><a href="view_forum.php?forum_id=<?php echo $row_forums['forum_id']; ?>"><?php echo $row_forums['forum_name']; ?></a></td>
   <td width="100"><div align="center"><?php echo $total_topics ?></div></td>
   <td width="100"><div align="center"></div></td>
  </tr>
  <?php } while ($row_forums = mysql_fetch_assoc ($forums)); ?>
</table>

Any help is appreciated. Many thanks!

Dani AI

Generated

Short version: the aggregate needs to be by forum, not by topic, and you must use the aggregated result keyed by forum_id when rendering each forum row. was on the right track — grouping by forum_id returns one count per forum. The symptom you saw (same number for every forum) often comes from using mysql_num_rows on the wrong result set (that returns how many rows the query returned, not the number of topics for the currently-displayed forum) or from grouping incorrectly and/or using an INNER JOIN (which hides forums with zero topics).

A reliable pattern is to run a single aggregate query that returns forum_id => topics_count, load that into an associative array, then use that array inside your forums loop. Example (SQL then PHP):

SELECT f.forum_id, f.forum_name, COUNT(t.topic_id) AS topics_count
FROM forums AS f
LEFT JOIN topics AS t ON t.topic_forum = f.forum_id
GROUP BY f.forum_id, f.forum_name
ORDER BY f.forum_id;
$topic_counts = array();
$res = mysql_query($sql);
while ($r = mysql_fetch_assoc($res)) {
  $topic_counts[$r['forum_id']] = (int)$r['topics_count'];
}

/* when outputting each forum row:
   echo isset($topic_counts[$row_forums['forum_id']]) ? $topic_counts[$row_forums['forum_id']] : 0;
*/

Notes and tips: use COUNT(t.topic_id) with a LEFT JOIN so forums with zero topics yield 0; include the non-aggregated columns in GROUP BY to satisfy SQL modes like ONLY_FULL_GROUPBY; avoid running a separate COUNT query inside the display loop for every forum (slow); and move away from deprecated `mysql*functions to MySQLi or PDO for newer PHP versions. See the MySQL COUNT documentation and the PHP manual formysql_num_rows` for details: MySQL COUNT docs PHP mysql_num_rows.

Recommended Answers

All 2 Replies

Hi Eiolin

I know a good resource of solving these types of issues.
<url snipped>

Hope it's helpful to you!

[wilkin]

I don't think advertisements were what he was looking for... and that site has to be the most ridiculous I have seen in a long time! Thanks for the laugh anyway! :)

Are you trying to find count against each topic or each forum.

If agianst each forum, then perhaps your query should be:

SELECT 
	forum_id, 
	count(*) 
FROM 
	topics 
LEFT JOIN forums 
	ON (topics.topic_forum = forums.forum_id) 
GROUP BY forum_id
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.