PHP MySQL - 从select语句计算行数

时间:2015-10-16 03:54:51

标签: php mysql

我正在尝试创建一个讨论区,并试图显示每个论坛中的主题数和总帖子数。我认为这个问题存在于我的WHERE条款中,因为这导致我在之前的问题上遇到过问题。

我知道不应该依赖rowCount(),而且在我的情况下无论如何都不起作用,这主要是因为它不能使用SELECT语句

我查看了SELECT count(*)个语句以及$row = $stmt->fetchAll(); $numrows = count($row)但是,我没有让它们中的任何一个工作。

<?php
        /* CATEGORIES */
            /* SELECTS ALL OF THE CATEGORIES OF THE BOARD */

            $query = "SELECT * FROM bkg_categories";

            try {
                $stmt = $db->prepare($query);
                $result = $stmt->execute();
            } catch(PDOException $e) {
                $error[] = "An error has occured. Please try again later.";
            }
            $categories = $stmt->fetchAll();

        /* FORUMS */
            /* SELECT ALL OF THE FORUMS IN EACH CATEGORY FOR THE BOARD */
            foreach($categories as $category) {
                $catid = $category['category_id'];

                $query = "SELECT * FROM bkg_forums WHERE category_id = :catid";
                $query_params = array(':catid' => $catid);

                try {
                    $stmt = $db->prepare($query);
                    $result = $stmt->execute($query_params);
                } catch(PDOException $e) {
                    $error[] = "An error has occured. Please try again later.";
                }
                $forums[$catid] = $stmt->fetchAll();

                foreach($forums[$category['category_id']] as $forum) {
                                $query = "SELECT * FROM bkg_topics where forum_id = :forumid";
                                $query_params = array(':forumid' => $forum['forum_id']);

                                try {
                                    $stmt = $db->prepare($query);
                                    $result = $stmt->execute($query_params);
                                } catch(PDOException $e) {
                                    $error[] = "An error has occured. Please try again later.";
                                }
                                $rows3['forumid'] = $stmt->fetchAll();
                                $rowCount = count($rows3['forumid']);
                                echo $rowCount;
                                var_dump($rowCount);
                            }



            }
        /* END QUERIES */
    ?>

这些是我正在使用的所有选择查询。在我的PHP选择查询的最后一个foreach循环中,你可以看到我正在玩的内容,var_dump()echo的工作,当它不在任何foreach循环中时,除了初级父母,但是一旦我将它移到我的桌子上,它似乎无法发挥作用。

<?php foreach($categories as $category): ?>
                <table class="forumtable">
                    <tr>
                        <td colspan="2"><strong><?php echo $category['category_name']; ?></strong></td>
                        <td><strong>Lastest Post</strong></td>
                        <td><strong>Topics</strong></td>
                        <td><strong>Posts</strong></td>
                    </tr>
                <?php foreach($forums[$category['category_id']] as $forum): ?>
                    <tr>
                        <td width="5%" class="forum"></td>
                        <td class="forum no-padding">
                            <a href="viewforum.php?f=<?php echo $forum['forum_id']; ?>"><?php echo $forum['forum_name']; ?></a>
                            <div class="description"><?php echo $forum['forum_desc']; ?></div>
                        </td>
                        <td width="15%"  class="forum content">    
                            <?php if($forum['forum_last_post_topic_id'] == 0): ?>
                                <div>No posts...</div>
                            <?php else: ?>
                                <?php if($forum['forum_last_post_id'] == 0): ?>
                                    <a href="viewtopic.php?f=<?php echo $forum['forum_id']; ?>&t=<?php echo $forum['forum_last_post_topic_id']; ?>"><?php echo substr($forum['forum_last_post_title'], 0, 10); ?></a>
                                <?php else: ?>
                                    <a href="viewtopic.php?f=<?php echo $forum['forum_id']; ?>&t=<?php echo $forum['forum_last_post_topic_id']; ?>#<?php echo $forum['forum_last_post_id']; ?>"><?php echo substr($forum['forum_last_post_title'], 0, 10); ?></a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                        <td width="5%"  class="forum content">
                            <?php 
                             echo $forum['forum_topics']; ?></td>
                        <td width="5%"  class="forum content"><?php 
                             echo $forum['forum_posts']; ?></td>
                    </tr> 
                    <?php endforeach; ?>                    
                </table>
            <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

我终于可以让select(*)工作了。

$db->query('SELECT COUNT(*) FROM users')->fetchColumn();就是我过去需要工作的东西。