2011-03-12 67 views
1

我有一個嚴重的問題在這裏。我正在開發一個論壇,並試圖獲得「未讀帖子」功能。然而,由於某種原因,我的代碼決定循環&得到荒謬的CPU使用率超過我的dedi。我從我的MySQL進程列表中看到的是,相同的sql不斷循環。代碼循環和崩潰服務器。不知道發生了什麼

這是我的代碼:

public function __construct() 
{ 
    $this->getUnread(); 
} 

private function getUnread() 
{ 
    global $cms; 
    // check unread threads 
    $forumSQL = $cms->db->query('SELECT * FROM forum_for'); 
    while($forums = $forumSQL->fetch_assoc()) 
    { 
     $forumId = $forums['id']; 
     $forumArray[$forumId] = $forums;    
    } 

    if($unreadThreadsSql = $cms->db->query('SELECT * FROM forum_threads WHERE posted > "' . $_SESSION['lastVisit'] . '"')) 
    { 
     while(!$unreadThreads = $unreadThreadsSql->fetch_assoc()) 
     { 
      $forumId = $unreadThreads['forId']; 
      if($this->checkUnread($unreadThreads['id'])) 
      { 
       $cms->db->query(' 
        INSERT INTO 
         forum_unread(
          threadID, 
          catID, 
          forumID, 
          userID, 
          datetime, 
          threadtime) 
        VALUES(
          '.$unreadThreads['id'].', 
          '.$forumArray[$forumId]['cat_id'].', 
          '.$forumId.', 
          '.$_SESSION['userId'].', 
          NOW(), 
          "'.$unreadThreads['posted'].'")'); 
      } 
     } 
    } 

    // check unread posts 

    if($unreadPostsSql = $cms->db->query('SELECT * FROM forum_posts WHERE datetime > "' . $_SESSION['lastVisit'] . '"')) 
    { 
     while($unreadPosts = $unreadPostsSql->fetch_assoc()) 
     { 
      $threadSql = $cms->db->query('SELECT * FROM forum_threads WHERE id = ' . $unreadPosts['threadId']); 
      $thread = $threadSql->fetch_assoc(); 
      $forumId = $thread['forId']; 
      if(!$this->checkUnread($thread['id'])) 
      { 
       $cms->db->query(' 
        INSERT INTO 
         forum_unread(
          threadID, 
          catID, 
          forumID, 
          userID, 
          datetime, 
          threadtime) 
        VALUES(
          '.$thread['id'].', 
          '.$forumArray[$forumId]['cat_id'].', 
          '.$forumId.', 
          '.$_SESSION['userId'].', 
          NOW(), 
          "'.$thread['posted'].'")'); 
      } 
     } 
    } 
} 

private function checkUnread($id) 
{ 
    global $cms; 
    if($unreadSql = $cms->db->query('SELECT * FROM forum_unread WHERE threadID = ' .$id . ' AND userID = ' . $_SESSION['userId'])) 
    { 
     if($unreadSql->num_rows == 1) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     }   
    } 
    else 
    { 
     return false; 
    } 
} 

真的可以使用一些幫助在這裏。我真的找不到爲什麼它循環,有沒有人有一個想法...

對於誰想知道,它似乎代碼循環功能checkUnread()。

作爲一個額外的注意,當註釋$ this-> getUnread();在構造函數中,整個事物停止超載。所以我確定它在這個代碼中。

+2

貴'while'終止循環,你希望它? – alex 2011-03-12 16:48:32

回答

2

您的意思是while($unreadThreads = $unreadThreadsSql->fetch_assoc())

順便說一句,如果你編寫了一個大的INSERT查詢,然後執行一次,而不是所有這些小小的查詢,它將是高效率。

例子:

$inserts = Array(); 
$unreadThreadsSql = $cms->db->query('SELECT * FROM forum_threads WHERE posted > "' . $_SESSION['lastVisit'] . '"') or die($cms->db->error); 
while ($unreadThreads = $unreadThreadsSql->fetch_assoc()) { 
    $forumId = $unreadThreads['forId']; 
    if (!$this->checkUnread($unreadThreads['id'])) 
     continue; 

    $inserts[] = '(' . 
     $unreadThreads['id'] . ',' . 
     $forumArray[$forumId]['cat_id'] . ',' . 
     $forumId . ',' . 
     $_SESSION['userId'] . ',' . 
     'NOW(),' . 
     '"' . $unreadThreads['posted'] . '")' 
    ); 
} 

if (count($inserts)) { 
    $cms->db->query('INSERT INTO forum_unread(
         threadID, 
         catID, 
         forumID, 
         userID, 
         datetime, 
         threadtime) VALUES' . 
        implode(',', $inserts)) or die($cms->db->error); 
} 
+1

好吧,事實證明,如果你向其他人展示你的代碼,你會發現最愚蠢的錯誤。謝謝,那!根本不應該在那裏。 – 2011-03-12 16:54:15

+0

關於db查詢。 我從mysqli文檔中瞭解到,它們的功能一次不支持多個查詢。如果這是錯的,那麼我可能會改變它。 – 2011-03-14 11:42:57

+0

@DaveSiegel:這可能是真的,但它不相關,因爲我一次不執行多個查詢。我使用多行執行_one_ ['INSERT'語句](http://dev.mysql.com/doc/refman/5.5/en/insert.html)。 – 2011-03-14 12:13:08

相關問題