php - Exhausting Memory - Tried Fixing Loops, Still Does Not Work -
i'm experiencing memory usage issue - cannot figure out where. i've tried replacing of foreach loops loops or issuing query db, still gettting same error - "fatal error: allowed memory size of 134217728 bytes exhausted (tried allocate 72 bytes) in on line 109". can provide insight may causing issue? thank you!
code after @patrick 's answer:
$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 demo_times_table sex = 'm' , (time_standard = 'a' or time_standard = 'b') order event, time asc"; $select_times_query = mysql_query($select_times_sql); //create array 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 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 8 cuts, take them if (($swimmer_info["time_standard"] == "a") || ($swimmer_info["time_standard"] == "b")) { //check if there 8 or less entries in current event, or if swim cut if ((count($event_standings[$current_event]) < 8) || ($swimmer_info["time_standard"] == "a")) { //add swimmer list of invites $event_standings[$current_event][] = $swimmer_info; //keep identifying information 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 appears in list if (!in_array($condensed_swimmer_info, $participating_swimmers)) { //it unique user - add them list $participating_swimmers[] = $condensed_swimmer_info; } } else { //add qualifying time did not fit list 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 $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 identifying information 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 appears in list if (!in_array($condensed_swimmer_info, $participating_swimmers)) { //it unique user - add them list $participating_swimmers[] = $condensed_swimmer_info; } //remove time 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 $loe => $event_swimmer) { echo "<h1>",$loe,"</h1><br />"; foreach ($event_swimmer $es) { echo $es["time"]," ",$es["name"]," ",$es["team"],"<br />"; } }
large data in database problem 95% ! - try using limit x,y
in queries , , put queries in loop . - see http://php.net/manual/en/function.mysql-free-result.php might help
<?php $participating_swimmers = array(); $event_standings = array(); $qualifying_times = array(); $select_times_sql = "select * demo_times_table sex = 'm' order time asc"; $select_times_query = mysql_query($select_times_sql); while ($select_times_row = mysql_fetch_assoc($select_times_query)) { //create array 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 8 cuts, take them if (($swimmer_info["time_standard"] == "a") || ($swimmer_info["time_standard"] == "b")) { //check if there 8 or less entries in current event, or if swim cut if ((count($event_standings[$current_event]) < 8) || ($swimmer_info["time_standard"] == "a")) { //add swimmer list of invites $event_standings[$current_event][] = $swimmer_info; //keep identifying information 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 appears in list if (!in_array($condensed_swimmer_info, $participating_swimmers)) { //it unique user - add them list $participating_swimmers[] = $condensed_swimmer_info; } } else { //add qualifying time did not fit list 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) demo_times_table 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 identifying information 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 appears in list if (!in_array($condensed_swimmer_info, $participating_swimmers)) { //it unique user - add them list $participating_swimmers[] = $condensed_swimmer_info; } //remove time 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) demo_times_table sex = 'm'"; $query = mysql_query($sql); while ($row = mysql_fetch_assoc($query)) { $loe = $row['event']; echo "<h1>".$loe."</h1><br />"; foreach ($event_standings[$loe] $es) { echo $es["time"]." ".$es["name"]." ".$es["team"]."<br />"; } } /* foreach ($participating_swimmers $ps) { echo $ps["name"]."<br /><br />"; } echo "<br /><br />"; */ ?>
Comments
Post a Comment