Php 可以在查询中使用查询来显示这样的类别吗? 函数getForums($id){ $currentHost=“http://$\u服务器[http\u主机]”; $query=“从类别中选择*”; 试一试{ 全球$db; $stmt=$db->prepare($query); $stmt->execute(); $result=$stmt->fetchAll(); foreach($结果为$行){ $category_title=$row['category_title']; $category_id=$row['category_id']; 回声' “.$category_title” '; $query2=“从类别id=”为“$category\u id.”的论坛中选择*; 试一试{ 全球$db; $stmt2=$db->prepare($query2); $stmt2->execute(); $result2=$stmt2->fetchAll(); foreach($result2作为$row2){ $forum_cat_id=$row2['category_id'; $forum_id=$row2['forum_id']; $forum_title=$row2['forum_title']; $forum_topic_count=$row2['forum_topic_count']; $forum_post_count=$row2['forum_post_count']; $forum_last_topic_id=$row2['forum_last_topic_id']; $forum_last_topic=$row2['forum_last_topic']; $forum_last_date=$row2['forum_last_date']; $forum_last_user=$row2['forum_last_user']; $fixed_last_topic=substr($forum_last_topic,0,25)。“…”; 回声' “.$forum\u topic\u count” “.$forum\u post\u count” 在“$forum\u last\u date”之前 '; } } }//这里有个错误! } 捕获(PDO异常$ex){ 死亡(“错误”); } }

Php 可以在查询中使用查询来显示这样的类别吗? 函数getForums($id){ $currentHost=“http://$\u服务器[http\u主机]”; $query=“从类别中选择*”; 试一试{ 全球$db; $stmt=$db->prepare($query); $stmt->execute(); $result=$stmt->fetchAll(); foreach($结果为$行){ $category_title=$row['category_title']; $category_id=$row['category_id']; 回声' “.$category_title” '; $query2=“从类别id=”为“$category\u id.”的论坛中选择*; 试一试{ 全球$db; $stmt2=$db->prepare($query2); $stmt2->execute(); $result2=$stmt2->fetchAll(); foreach($result2作为$row2){ $forum_cat_id=$row2['category_id'; $forum_id=$row2['forum_id']; $forum_title=$row2['forum_title']; $forum_topic_count=$row2['forum_topic_count']; $forum_post_count=$row2['forum_post_count']; $forum_last_topic_id=$row2['forum_last_topic_id']; $forum_last_topic=$row2['forum_last_topic']; $forum_last_date=$row2['forum_last_date']; $forum_last_user=$row2['forum_last_user']; $fixed_last_topic=substr($forum_last_topic,0,25)。“…”; 回声' “.$forum\u topic\u count” “.$forum\u post\u count” 在“$forum\u last\u date”之前 '; } } }//这里有个错误! } 捕获(PDO异常$ex){ 死亡(“错误”); } },php,html,mysql,css,Php,Html,Mysql,Css,我在代码中标记了一个错误,大约有7行。 我有另一个问题,但一直没有得到回答,基本上我正在尝试创建我的论坛,以便所有子类别(论坛)都进入该类别,而不是每次都创建一个全新的论坛div。您还没有为第二次尝试编写任何捕获。因此,代码将以等待捕获的错误结束。 这样更改代码 function getForums($id) { $currentHost = "http://$_SERVER[HTTP_HOST]"; $query = "SELECT * FROM categories";

我在代码中标记了一个错误,大约有7行。
我有另一个问题,但一直没有得到回答,基本上我正在尝试创建我的论坛,以便所有子类别(论坛)都进入该类别,而不是每次都创建一个全新的论坛div。

您还没有为第二次尝试编写任何捕获。因此,代码将以等待捕获的错误结束。 这样更改代码

function getForums($id) {
    $currentHost = "http://$_SERVER[HTTP_HOST]";
    $query = "SELECT * FROM categories";
    try {
        global $db;
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $result = $stmt->fetchAll();
        foreach($result as $row) {
            $category_title = $row['category_title'];
            $category_id = $row['category_id'];
            echo '<div class="forum pleft">
                    <div class="forum-header">
                        <span class="header-text">'.$category_title.'</span>
                    </div>
                    <table>';
            $query2 = "SELECT * FROM forums WHERE category_id='".$category_id."'";
            try {
                global $db;
                $stmt2 = $db->prepare($query2); 
                $stmt2->execute();
                $result2 = $stmt2->fetchAll();
                foreach($result2 as $row2) {
                    $forum_cat_id = $row2['category_id'];
                    $forum_id = $row2['forum_id'];
                    $forum_title = $row2['forum_title'];
                    $forum_topic_count = $row2['forum_topic_count'];
                    $forum_post_count = $row2['forum_post_count'];
                    $forum_last_topic_id = $row2['forum_last_topic_id'];
                    $forum_last_topic = $row2['forum_last_topic'];
                    $forum_last_date = $row2['forum_last_date'];
                    $forum_last_user = $row2['forum_last_user'];
                    $fixed_last_topic = substr($forum_last_topic,0,25).'...';
                    echo '<tr>
                            <td class="title"><a href="'.$currentHost.'/forums/view-forum/index.php?cid='.$forum_cat_id.'&fid='.$forum_id.'">'.$forum_title.'</a></td>
                            <td class="topics">'.$forum_topic_count.'</td>
                            <td class="posts">'.$forum_post_count.'</td>
                            <td class="lastpost"><a href="'.$currentHost.'/forums/view-thread/index.php?cid='.$id.'&fid='.$forum_id.'&tid='.forum_last_topic_id.'">'.$fixed_last_topic.'</a> by <a href="'.$currentHost.'/users/index.php?username='.$forum_last_user.'">'.$forum_last_user.'</a> at '.$forum_last_date.'</td>
                        </tr>
                    </table>
                </div>';
                }
            }
        } // Getting an error here!
    }
    catch(PDOException $ex) {
        die("error");
    }
}

哪个是错误消息?几乎可以肯定这是一个括号问题。几乎在任何时候都有嵌套查询,并且内部查询使用外部查询的值进行
where
筛选,您可能可以/应该使用单个
join
ed查询重新编写。看起来您没有用CATCH结束TRY块。我只是添加了一个CATCH pdoexeception,但仍然得到相同的错误。错误在于我在第44行(我在那里标记了它)添加了它,但似乎我没有上传它。对不起,伙计们,谢谢!
}
catch(PDOException $ex) {
    die("error");
}
} // Getting an error here!