从JOIN查询获取输出的问题


Issues with getting output from JOIN query

我正在尝试一个连接,如果它结构正确,我不是积极的,但它没有失败。我正在做的查询是$query2。我试图SELECT匹配,其中form_categories表的id等于forum_topics表中的category_id,并计算有多少。基本上,如果在某个类别中有10个不同的主题,我希望显示10个。

我的代码在我试图输出信息的地方正在死亡。它从我的页面上消失了。

如果我的查询在我想做的事情中是正确的,这是失败的输出…

<?php $numrows2 = mysqli_num_rows($query2);?>
<?php if ($numrows2 >= 0) : ?>
    <?php while ($row2 = mysqli_fetch_assoc($query2)) : ?>
        <?php $threads = $row['tid2']; ?>
        <div class="discussions_right">
        <p>Threads<?php echo $threads; ?></p>
    <?php endwhile; ?>
<?php endif; ?> 

完整代码
$query = mysqli_query($con,"SELECT * FROM forum_categories ORDER BY category_section ASC, category_title ASC")
or die ("Query failed: %s'n".($query->error));
$query2 = mysqli_query($con,"SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t JOIN forum_categories AS c")
or die ("Query2 failed: %s'n".($query2->error));
?>
<?php if(mysqli_num_rows($query) > 0): ?>
<div class="discussions_table">
    <?php while($row = mysqli_fetch_assoc($query)): ?>
        <?php if($row['category_section'] !== $category_section): ?>
            <?php $category_section = $row['category_section'] ?>
            <div class="category_section">
                <?= $category_section ?>
            </div>
        <?php endif; ?>         
        <div class="category_border">
            <div class="discussions_left">
                <div class="discussions_category_title">
                    <a href="forum_view_category.php?cid=<?= $row['id'] ?>">
                        <?= $row['category_title'] ?>
                    </a>
                </div>
                <div class="discussions_category_description">
                    <?= $row['category_description'] ?>
                </div>
            </div>
            <?php $numrows2 = mysqli_num_rows($query2);?>
            <?php if($numrows2 >= 0):?>
                <?php while($row2 = mysqli_fetch_assoc($query2)): ?>
                    <?php $threads = $row['tid2']; ?>
            <div class="discussions_right">
                <p>Threads<?php echo $threads; ?></p>
                <?php endwhile; ?>
            <?php endif; ?> 
                <p>Posts</p>
            </div>
        </div>      
    <?php endwhile; ?>
<?php else: ?>
    <p>There are no posted categories available yet.</p>
<?php endif; ?>

我做错了什么?

UPDATE:显示表结构

CREATE TABLE `forum_categories` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `category_section` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `category_title` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
 `category_description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `last_post_date` datetime DEFAULT NULL,
 `last_user_posted` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

forum_topics
CREATE TABLE `forum_topics` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `category_id` int(11) NOT NULL,
 `topic_title` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
 `topic_creator` int(11) NOT NULL,
 `topic_last_user` int(11) DEFAULT NULL,
 `topic_date` datetime NOT NULL,
 `topic_reply_date` datetime NOT NULL,
 `topic_views` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

您测试过这个查询了吗?

SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t JOIN forum_categories AS c

我认为它不像你期望的那样。我可能错了。

编辑:

Try this query

SELECT t.*, COUNT(c.id) AS tid FROM forum_topics AS t INNER JOIN forum_categories AS c ON t.category_id = c.id