user6494551
user6494551

Reputation:

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>";

Upvotes: 0

Views: 180

Answers (1)

Drone
Drone

Reputation: 1134

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
}

Upvotes: 0

Related Questions