add GROUP BY t.ID to the end of your query.
-r-
Main Topics
Browse All TopicsHi,
I have this sql_query code on my message board front page:
$amount = mysql_query("SELECT p.*, t.ID FROM posts p, topics t WHERE p.TopicID=t.ID");
$num_rows = mysql_num_rows($amount);
and to print it out I have the <?php echo $num_rows ?>
The 2 tables looks the following way:
Table posts:
ID int(5) PRI NULL auto_increment
TopicID int(5) 0
timestamp timestamp(14) YES NULL
user_ varchar(10) YES NULL
Post text
Table topics
ID int(5) PRI NULL auto_increment
TopicName varchar(50)
and what I'm trying to achieve is to display the amount of posts each topic holds, but with that current search it displays the amount of all the posts made..
any ideas?
Thanks,
-T
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, well in phpmyadmin this query shows the right result:
SELECT count( p . ID ) as posts , t . ID FROM posts p , topics t WHERE p . TopicID = t . ID Group by t . ID LIMIT 0, 30;
it gives the right result:
posts ID
3 1
5 2
3 3
4 4
But when using with php:
$amount = mysql_query("SELECT count( p . ID ) as posts , t . ID FROM posts p , topics t WHERE p . TopicID = t . ID Group by t . ID LIMIT 0, 30");
$num_rows = mysql_num_rows($amount);
And echoing it out <?php echo $num_rows ?> it still displays the amount of topic and that being four??
-T
Thanks for your reply, now it displays the right amount of posts, but it also displays all the topics 4 times, here's my code:
$amount = mysql_query("SELECT distinct count(p.ID) as posts, t.ID FROM posts p, topics t WHERE p.TopicID = t.ID Group by t.ID LIMIT 0, 30");
while($row = mysql_fetch_array($amount)
$topic_query = mysql_query("SELECT t.id, t.topicname AS topicname, DATE_FORMAT(MAX(p.timestam
while ($topic = mysql_fetch_array($topic_q
?>
<TR>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
<?php } ?>
</TD>
</TR>
<?php
}
?>
Some bugs somewhere in the code I assume:)
-T
That's because you're putting the second while loop within the first one. Try this:
$amount = mysql_query("SELECT distinct count(p.ID) as posts, t.ID FROM posts p, topics t WHERE p.TopicID = t.ID Group by t.ID LIMIT 0, 30");
$posts = array();
while($row = mysql_fetch_array($amount)
$posts[$row['ID']] = $row['posts'];
}
$topic_query = mysql_query("SELECT t.id, t.topicname AS topicname, DATE_FORMAT(MAX(p.timestam
_ GROUP BY t.id ORDER BY p.timestamp;");
while ($topic = mysql_fetch_array($topic_q
?>
<TR>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
</TD>
<TD>
<span class="member_links_underl
</TD>
</TR>
<?php
}
?>
Business Accounts
Answer for Membership
by: TeRReFPosted on 2007-01-11 at 05:24:50ID: 18292218
Try this:
$amount = mysql_query("SELECT p.*, t.ID FROM posts p, topics t WHERE p.TopicID=t.ID GROUP BY t.ID");