2015-05-11 57 views
-1
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"); 
    } 
} 

我在代碼中標記了出現錯誤的地方,大概有7行。 我有另一個問題,但從來沒有得到答案,基本上我試圖讓我的論壇,以便所有的子類別(論壇)進入類別,並不會每次都做一個全新的論壇div。查詢內部查詢是否可以顯示這樣的類別?

+0

哪一條是錯誤信息?幾乎可以肯定這是一個括號問題。 – jcoppens

+2

幾乎在任何時候你有嵌套查詢,並且內部查詢使用外部查詢的值來進行'where'過濾,你可能應該/應該使用SINGLE'join'ed查詢來重寫。 –

+0

看起來你沒有用CATCH結束TRY塊。 –

回答

2

你還沒有寫出第二次嘗試。所以代碼將以等待捕獲的錯誤結束。 以這種方式更改代碼

} 
catch(PDOException $ex) { 
    die("error"); 
} 
} // Getting an error here! 
+0

我已經添加了它,但似乎我沒有上傳它。對不起,夥計,謝謝! –

相關問題