2013-12-16 10 views
0

나는이 문제에 대해 정신적으로 다가갔습니다. 나는 이것을 한 번도 해본 적이 없다고 확신하지만, 뭔가 바꾸어 놓았을 것입니다. 배열에 추가 할 때 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

그건 항상 동일한 오류 - 동일 할당, 같은 라인을 . 움직이는 것을 시도해 보았습니다. 객체가 아닌 배열을 사용하려고했습니다.

에 대한 아이디어는 왜 많은 양의 메모리를 사용하려고합니까? 그것은이 라인의

ini_set('memory_limit','256M'); 
+1

난 당신이 작은 자기 문제를 가지고 예를 포함 할 좋습니다. 우리가 내부에서 필요로하지 않는 모든 HTML 등으로, 코드가 무엇인지 알아내는 것은 너무 많은 일이며, 이것은 당신이 쉽게 할 수있는 일입니다. http://sscce.org/을 참조하십시오. – Nanne

+0

@ Nanne 코드 섹션이 독자적으로 이해되지 않을까 걱정되었습니다. 다음 시간 동안 주목 받았다. –

답변

3

당신에게 문제를 일으키는 :

는 php.ini 파일 또는 스크립트의 사용 메모리 제한을 늘려보십시오

+0

Doh! 나는 젠드 스튜디오 때문에 그것을 추가했다 ...그것을 제거하고 오류가 사라졌습니다. 그렇게 간단한 것 ... –

1

(약 데이터베이스에 400 개 행이 있습니다)

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의 타입 저글링은 부울 false로 레코드의 끝에서 널 (null)을 변환합니다.

+0

저는 PHP.ini에서이 개념을 두 번째로 사용합니다 : memory_limit = 256M – tremor

+1

코드에 무한 루프가 있습니다. 더 많은 메모리를 추가하면 어떻게됩니까? – andrewsi

+0

네 말이 맞아, 나는 그걸 바로 잡지 않았다. 감사. :) – MElliott

0

PHP로 작업을 복제하고 있습니다.

조회 후 을하고있는 동안 당신의 결과는 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>"); 
}