Micah Morris Micah Morris - 3 months ago 17
PHP Question

PHP while loop memory error

I'm getting this error:


Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate > 32 bytes) in path/to/1phpquery.php on > line 24


Line 24 is


while ($r = $mysqli->query($query)) {


The full code is:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$instr = "CCBOT";
require("dbconnect.php");

$query = 'SELECT LgSpecNet, SmSpecNet, CommNet FROM cot WHERE Ticker = "$instr" LIMIT 5';

if ($result = $mysqli->query($query)) {

$table = array();

$table['cols'] = array(
array('label' => 'Large Spec Net', 'type' => 'number'),
array('label' => 'Small Spec Net', 'type' => 'number'),
array('label' => 'Commercial Net', 'type' => 'number')
);

$rows = array();

while ($r = $mysqli->query($query)) {
$temp = array();

$ra = $r->fetch_assoc();

$temp[] = array('v' => (int) $ra['LgSpecNet']);
$temp[] = array('v' => (int) $ra['SmSpecNet']);
$temp[] = array('v' => (int) $ra['CommNet']);

$rows[] = array('c' => $temp);
}

$result->free();

$table['rows'] = $rows;

$jsonTable = json_encode($table);

echo $jsonTable;

}

$mysqli->close();

?>

Answer

you're endlessly re-running your query. Change

while ($r = $mysqli->query($query))

To

while ($ra = $result->fetch_assoc())

And remove the line:

$ra = $r->fetch_assoc();   
Comments