代码循环&崩溃的服务器。不知道发生了什么

时间:2011-03-12 16:41:01

标签: php mysql sql

我在这里遇到了严重的问题。我正在开发一个论坛,并尝试准备好“未读帖子”功能。但是,出于某种原因,我的代码决定循环播放在我的dedi上获得荒谬的CPU使用率。我从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()时;在构造函数中,整个事情停止重载。所以我确定这是在这段代码中。

1 个答案:

答案 0 :(得分: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);
}