我有一個嚴重的問題在這裏。我正在開發一個論壇,並試圖獲得「未讀帖子」功能。然而,由於某種原因,我的代碼決定循環&得到荒謬的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();在構造函數中,整個事物停止超載。所以我確定它在這個代碼中。
貴'while'終止循環,你希望它? – alex 2011-03-12 16:48:32