Dakota Wagner Dakota Wagner - 4 months ago 18
MySQL Question

Trying to import SQL result into array: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted

I know other people have gotten this error, and have read threads to try and track down the issue, but my code is too different from other cases. There are about 150 records that should be returned from this specific query.

Below is my code that is causing the error:

$sql = "SELECT * FROM donor WHERE DonationAmount = 1000 AND Category = '1' or DonationAmount = 1000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
$data = array(); // create a variable to hold the information
while (($row = mysqli_fetch_array($result, MYSQL_ASSOC)) !== false){
$data[] = $row; // add the row in to the results (data) array
}


I don't know why I'm getting this error!

EDIT: this is what I'm trying to do with said array.

$counter = 2;
$divisorCount = ceil($counter/2);
$forEachCount = 1;
foreach ($data as $row){
$forEachCount++;
$block[] = "<div class='block'>".$row['DisplayName']."</div>\n";
if($forEachCount > $divisorCount){
$forEachCount = 0;
end($block);
$key = key($block);
$block[$key] .= "</div><div class='column'>"; // the insert
}
}
unset($row,$key,$forEachCount,$divisorCount); //cleanup

$output = "<div class='tableContainer'>
<div class='column'>".implode($block)."</div>
</div>";

Answer

Replace your code with this, here while loop creating infinite loop, also check query conditions

$sql = "SELECT * FROM donor WHERE (DonationAmount = 1000 AND Category = '1') or (DonationAmount = 1000 AND Category IS NULL) ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);
$data = array(); // create a variable to hold the information
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
  $data[] = $row; // add the row in to the results (data) array
}