duangonglian6028 2013-12-24 10:09
浏览 54
已采纳

我的PHP内存为何以及如何耗尽? [重复]

I am getting the error

PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 33292313 bytes)

when i am running my pagination function and i don't know what is happening and/or why i'm getting the error.

From what i've seen on the other questions here, most people recommend that i change the memory limit in the php.ini file.However, i would like to know why this error is happening and what i can do to prevent it form happening in the future.

From this question Meaning of "Fatal error: Allowed memory size of x bytes exhausted (tried to allocate y bytes)"? i can see that the php script has exceeded the allocated memory limit (duh!) but it isn't really helpful.

Question:

  • Isn't php simply a language that interacts with a database? Why then does it need memory to run?
  • What causes the error?(i know that there isn't enough memory, but why is there so much memory used up)
  • What can i do to prevent it from happening again

My Pagination function:

function Pagination( $connection, $num, $page, $view  ) {

    $view=$view;

    //Results per page
    $pagerows=10;

    //Tells us the last page number
    $last=ceil( $num/$pagerows );

    // Establish the $PaginationNav variable(used to navigate the paginated results)
    $PaginationNav = '';

    //Check how many pages of results there are
    //If there is more than 1 page
    if( $last != 1 ) {

        //Check if we are on page 1, if so we don't need the 'previous' link
        //We are on page one
        if( $page == 1 ) {

            //Adds the current page
            $PaginationNav .= $page;

            //Adds the next 3 pages to the pagination
            for( $i=$page+1;$i<$last;$i++ ) {
                $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$i\">$i</a>";
                if( $i > $page+4 ) {
                    break;
                }
            }

            $PaginationNav .= "...";

            //Adds the last page to the pagination
            $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$last\">$last</a>";

            //Adds the next page button
            $next=$page + 1;

            $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$next\">Next </a> ";
        } else {
            //we are not on page 1

            //Adds the previous button to pagination
            $previous=$page - 1;

            $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$previous\">Previous</a>";

            //Adds the current page
            $PaginationNav .=$page;

            //Adds the left navigations
            for( $i = $page-4;$i=$page;$i++ ) {
                if( $i>0 ) {
                    $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$i\">$i</a>";
                }
            }

            //Adds the right navigations
            for( $i=$page;$i<$last;$i++ ) {
                $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$i\">$i</a>";
                if( $i > $page+4 ) {
                    break;
                }
            }

            //Adds the last page to the pagination
            $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$last\">... $last</a>";

            //Adds the next page button
            $next=$page + 1;

            $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$next\">Next </a> ";
        }
    }

    //There is only one page
    if( $last == 1 ) {
        $PaginationNav .= $page;
    }

    return $PaginationNav;
}

There was a comment on one of the question i've seen that states that simply increasing the memory limit isn't a good substitute for good coding. I agree, and i would like to change that code to be better instead of simply mindlessly increasing memory.

P.S I've no formal training in programming, and am learning simply by throwing myself into coding, so i apologise if the answer is pretty obvious.

</div>
  • 写回答

2条回答 默认 最新

  • dongqiaogouk86049 2013-12-24 10:11
    关注

    you have:

     for($i = $page-4;$i=$page;$i++)   // change to ==
     {
         if($i>0)
         {
             $PaginationNav .= "<a class='Pagination' href=\"$_SERVER[PHP_SELF]?view=$view&page=$i\">$i</a>";
         }
     }
    

    you are assigning $page to $i in $i=$page and it will return $page (probably a positive number) every time, which will result in true condition. Change it to ==

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?