1

我一直在为这件事发愁。我敢肯定它从来没有这样做过,但是我一定改变了一些东西,所以它做到了。

这是有php问题的脚本:

<?php

echo <<<START
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Spotify Community Staff Tracker</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type='text/javascript' src='scripts/respond.min.js'></script>
</head>
<body>
START;

error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

require_once('config.php');

$posts = array();

$db = new mysqli($config['host'], $config['user'], $config['password'], $config['database']);

if ( ($result = $db->query('SELECT * FROM `posts`')) != false)
{
    while ( ($obj = mysqli_fetch_object($result)) !== false)
    {
        if (@$_GET['idfilter'] && @$_GET['idfilter'] != $obj->board)
        {
            continue;
        }

        $posts[] = array('datetime' => $obj->datetime, 'subject' => $obj->subject, 'post_url' => $obj->post_link, 'user_url' => $obj->author_link, 'user' => $obj->author_name);
    }

    if (sizeof($posts) == 0)
    {
        if ($_GET['idfilter'])
            die("Filter caused zero result, or cron hasn't run.");
        die("Cron hasn't been run.");
    }

}
else
{
    die("An error occured.");
}

$lupdate = mysqli_fetch_object($db->query("SELECT * FROM `last_update`"));

echo <<<BOTTOM
<div id="right" class="fixed">
    <p id="lastupdate">Last Updated: {$lupdate->timestamp}</p>
    <p><form id="filter" action="" method="get">
            <input type="text" placeholder="Enter a forum id to filter..." name="idfilter" />
            <input type="submit" value="Filter" id="submit" />
    </form>
            </p>
</div>
BOTTOM;

echo("\n<div id=\"posts\">");

foreach (array_reverse($posts) as $post)
{
    echo("\n<p><a class=\"postlink\" target=\"_blank\" href=\"{$post['post_url']}\">{$post['subject']}</a> - by <a class=\"suser\" target=\"_blank\" href=\"{$post['user_url']}\"><img src=\"http://spotify.i.lithium.com/html/rank_icons/spotify_icon_new.png\" alt=\"Spotify Staff\" />{$post['user']}</a> <span class=\"datetime\">on {$post['datetime']}</span>\n</p>");
}

echo("\n</div>");

echo <<<END
\n</body>
</html>
END;

?>

每当我运行它时,我都会收到以下错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes) in <filepath>\index.php on line 36

它总是相同的错误,-相同的分配,相同的行。我试过移动东西,我试过使用数组而不是对象,没用。

关于为什么要尝试使用如此大量的内存的任何想法?

(数据库中大约有 400 行)

4

3 回答 3

3

正是这条线导致了您的问题:

while ( ($obj = mysqli_fetch_object($result)) !== false)

如果您查看文档: http ://www.php.net/manual/en/mysqli-result.fetch-object.php

mysqli_fetch_object 如果有则返回下一行;如果没有,则为 null。因此,因为您正在进行严格的比较,所以您的循环将永远不会结束。要解决此问题,您可以简单地执行以下操作:

while ($obj = mysqli_fetch_object($result))

并让 PHP 的类型杂耍将记录集末尾的 null 转换为布尔值 false。

于 2013-12-16T19:11:45.173 回答
1

尝试在 php.ini 或此脚本中增加内存限制:

ini_set('memory_limit','256M');
于 2013-12-16T19:10:35.590 回答
0

您正在将工作复制到 PHP。

查询后,您正在对结果进行 while 迭代,并将存储在 PHP 数组中。而且,在脚本的最后,您正在迭代您的数组并将其放入 HTML 输出中。

我认为,您可以通过一次迭代完成所有操作,将您的foreach 帖子替换为:

while ( ($obj = mysqli_fetch_object($result)) !== false)
{
    if (@$_GET['idfilter'] && @$_GET['idfilter'] != $obj->board)
    {
        continue;
    }

    echo("\n<p><a class=\"postlink\" target=\"_blank\" href=\"{$post['post_url']}\">{$post['subject']}</a> - by <a class=\"suser\" target=\"_blank\" href=\"{$post['user_url']}\"><img src=\"http://spotify.i.lithium.com/html/rank_icons/spotify_icon_new.png\" alt=\"Spotify Staff\" />{$post['user']}</a> <span class=\"datetime\">on {$post['datetime']}</span>\n</p>");
}
于 2013-12-16T19:11:46.623 回答