我一直在思考這個問題。我相信它從來沒有用過這樣做,但是我必須改變一些東西,所以它確實如此。PHP腳本添加到數組時總是超出內存限制?
這是有問題的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行)
我建議你做一個小的,獨立的例子有問題。有了所有我們不需要的html等內容,找出你的代碼是多少工作,這是你可以輕鬆完成的。請參閱http://sscce.org/ – Nanne
@Nanne我擔心部分代碼本身沒有意義。但下次注意到。 –