php - 耗尽内存 - 尝试修复循环,仍然无法正常工作

原文 标签 php mysql memory memory-management

我目前遇到内存使用问题 - 但我不知道在哪里。我尝试用 for 循环替换我的一些 foreach 循环或向数据库发出另一个查询,但我仍然遇到相同的错误 - “ fatal error :允许的 134217728 字节的内存大小已用尽(试图分配 72 字节)在第 109 行"。任何人都可以就可能导致问题的原因提供一些见解吗?谢谢!

@Patrick 回答后的代码:

    $participating_swimmers = array();  
    $event_standings = array();
    $qualifying_times = array();

    $events = array();

        $current_event = '';
        $select_times_sql = "SELECT event, time, name, year, team, time_standard, date_swum
                            FROM demo_times_table
                            WHERE sex = 'M' AND (time_standard = 'A' OR time_standard = 'B')
                            ORDER BY event, time ASC";
        $select_times_query = mysql_query($select_times_sql);
        //Create array with the current line's swimmer's info
        while ($swimmer_info = mysql_fetch_assoc($select_times_query)) {
            if($current_event != $swimmer_info['event']){
                $events[] = $current_event = $swimmer_info['event'];
            }

            //Create array with the current line's swimmer's info
            $swimmer_info["time"] = $select_times_row['time'];
            $swimmer_info["name"] = $select_times_row['name'];
            $swimmer_info["year"] = $select_times_row['year'];
            $swimmer_info["team"] = $select_times_row['team'];
            $swimmer_info["time_standard"] = $select_times_row['time_standard'];
            $swimmer_info["date_swum"] = $select_times_row['date_swum'];

            //Create "Top 8" list - if more than 8 A cuts, take them all
            if (($swimmer_info["time_standard"] == "A") || ($swimmer_info["time_standard"] == "B")) {               
                //Check if there are 8 or less entries in the current event, or if the swim is an A cut
                if ((count($event_standings[$current_event]) < 8) || ($swimmer_info["time_standard"] == "A")) {
                    //Add swimmer to the list of invites
                    $event_standings[$current_event][] = $swimmer_info;

                    //Keep only the identifying information about the swimmer
                    $condensed_swimmer_info["name"] = $swimmer_info["name"];
                    $condensed_swimmer_info["year"] = $swimmer_info["year"];
                    $condensed_swimmer_info["team"] = $swimmer_info["team"];

                    //Check if swimmers name already appears in list
                    if (!in_array($condensed_swimmer_info, $participating_swimmers)) {
                        //It is a unique user - add them to the list
                        $participating_swimmers[] = $condensed_swimmer_info;
                    }
                } else {
                    //Add the qualifying time that did not fit into the list to a list of qualifying times
                    $qualifying_times[$current_event][] = $swimmer_info;    
                }
            }
        }

    //Sort each array of times in descending order

    arsort($event_standings);
    arsort($qualifying_times);
    $num_of_swimmers = count($participating_swimmers);

    while ($num_of_swimmers < 80) {
foreach ($events as $loe) {
    $num_of_qualifying_times = count($qualifying_times[$loe]);
    $swimmer_info = $qualifying_times[$loe][$num_of_qualifying_times-1];
    $event_standings[$loe][] = $swimmer_info;

     //Keep only the identifying information about the swimmer
    $condensed_swimmer_info["name"] = $swimmer_info["name"];
    $condensed_swimmer_info["year"] = $swimmer_info["year"];
    $condensed_swimmer_info["team"] = $swimmer_info["team"];            
    //Check if swimmers name already appears in list
    if (!in_array($condensed_swimmer_info, $participating_swimmers)) {
        //It is a unique user - add them to the list
        $participating_swimmers[] = $condensed_swimmer_info;
    }

    //Remove time from array of qualifying times
    unset($qualifying_times[$loe][$num_of_qualifying_times-1]);
}
$new_num_of_swimmers = count($participating_swimmers);
if($num_of_swimmers == $new_num_of_swimmers) break;
else $num_of_swimmers = $new_num_of_swimmers;
    }


    arsort($event_standings);
    arsort($qualifying_times);
    foreach($event_standings as $loe => $event_swimmer) {
    echo "<h1>",$loe,"</h1><br />"; 
    foreach ($event_swimmer as $es) {
        echo $es["time"],"  ",$es["name"],"  ",$es["team"],"<br />";
    }
    }

最佳答案

数据库中的大数据是 95% 的问题!
- 尝试使用 limit x,y在您的查询中,并将这些查询放在某个循环中。
- 见 http://php.net/manual/en/function.mysql-free-result.php它可能会有所帮助

<?php

    $participating_swimmers = array();  
    $event_standings = array();
    $qualifying_times = array();

    $select_times_sql = "SELECT *
                        FROM demo_times_table
                        WHERE sex = 'M'
                        ORDER BY time ASC";
    $select_times_query = mysql_query($select_times_sql);
    while ($select_times_row = mysql_fetch_assoc($select_times_query)) {
        //Create array with the current line's swimmer's info
        $swimmer_info["time"] = $select_times_row['time'];
        $swimmer_info["name"] = $select_times_row['name'];
        $swimmer_info["year"] = $select_times_row['year'];
        $swimmer_info["team"] = $select_times_row['team'];
        $swimmer_info["time_standard"] = $select_times_row['time_standard'];
        $swimmer_info["date_swum"] = $select_times_row['date_swum'];

        //Create "Top 8" list - if more than 8 A cuts, take them all
        if (($swimmer_info["time_standard"] == "A") || ($swimmer_info["time_standard"] == "B")) {               
            //Check if there are 8 or less entries in the current event, or if the swim is an A cut
            if ((count($event_standings[$current_event]) < 8) || ($swimmer_info["time_standard"] == "A")) {
                //Add swimmer to the list of invites
                $event_standings[$current_event][] = $swimmer_info;

                //Keep only the identifying information about the swimmer
                $condensed_swimmer_info["name"] = $swimmer_info["name"];
                $condensed_swimmer_info["year"] = $swimmer_info["year"];
                $condensed_swimmer_info["team"] = $swimmer_info["team"];

                //Check if swimmers name already appears in list
                if (!in_array($condensed_swimmer_info, $participating_swimmers)) {
                    //It is a unique user - add them to the list
                    $participating_swimmers[] = $condensed_swimmer_info;
                }
            } else {
                //Add the qualifying time that did not fit into the list to a list of qualifying times
                $qualifying_times[$current_event][] = $swimmer_info;    
            }
        }
    }

    mysql_free_result($select_times_query);
    //Sort each array of times in descending order

    arsort($event_standings);
    arsort($qualifying_times);
    $num_of_swimmers = count($participating_swimmers);


        $sql = "SELECT DISTINCT(event)
                FROM demo_times_table
                WHERE sex = 'M' limit 80";
        $query = mysql_query($sql);
        while ($row = mysql_fetch_assoc($query)) {
            $loe = $row['event'];
            $num_of_qualifying_times = count($qualifying_times[$loe]);
            $event_standings[$loe][] = $qualifying_times[$loe][$num_of_qualifying_times-1];

             //Keep only the identifying information about the swimmer
            $condensed_swimmer_info["name"] = $qualifying_times[$loe][$num_of_qualifying_times]["name"];
            $condensed_swimmer_info["year"] = $qualifying_times[$loe][$num_of_qualifying_times]["year"];
            $condensed_swimmer_info["team"] = $qualifying_times[$loe][$num_of_qualifying_times]["team"];            
            //Check if swimmers name already appears in list
            if (!in_array($condensed_swimmer_info, $participating_swimmers)) {
                //It is a unique user - add them to the list
                $participating_swimmers[] = $condensed_swimmer_info;
            }

            //Remove time from array of qualifying times
            unset($qualifying_times[$loe][$num_of_qualifying_times-1]);
        }
        $num_of_swimmers = count($participating_swimmers);
        mysql_free_result($query);


    arsort($event_standings);
    arsort($qualifying_times);
    $sql = "SELECT DISTINCT(event)
                FROM demo_times_table
                WHERE sex = 'M'";
    $query = mysql_query($sql);
    while ($row = mysql_fetch_assoc($query)) {
        $loe = $row['event'];
            echo "<h1>".$loe."</h1><br />"; 

            foreach ($event_standings[$loe] as $es) {
                echo $es["time"]."  ".$es["name"]."  ".$es["team"]."<br />";
            }
    }

    /*
    foreach ($participating_swimmers as $ps) {
        echo $ps["name"]."<br /><br />";    
    }
    echo "<br /><br />";
*/
?>

关于php - 耗尽内存 - 尝试修复循环,仍然无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4945641/

相关文章:

php - 在 Laravel 模型中更改数据库连接

php - 使用 PHP 从文件中读取大型 js 数组

mysql - centos 7 Chef mysql自定义目录

php - 获取当前插入的 ID 的 ID

mysql - 通过odbc和Excel调用mysql

php - 使用 Docker 和 WSL2 在 VSCode 中调试 PHP 时出现的问题

php - 解码PHP中的JavaScript转义序列(\x27,\x22等)

iphone - 多次按下时声音重叠

c++ - 如何在不占用CPU的情况下进行大量处理?

linux - ELF和内存中的段有什么区别