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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料