Reputation: 1163
I have a PHP project I'm making, but I've hit a dead end as one line is causing the script to error out saying the memory limit has been exausted.
The line in question is part of a method, as follows:
public function query_all($query)
{
if (function_exists('mysqli_fetch_all')) # Compatibility layer with PHP < 5.3
$res = mysqli_fetch_all($this->query($query));
else
for ($res = array(); $tmp = mysqli_fetch_array($this->query($query));) $res[] = $tmp;
return $res;
}
This function is part of a class named Db, the line with the error is the line with the for loop:
for ($res = array(); $tmp = mysqli_fetch_array($this->query($query));) $res[] = $tmp;
This function is only called once in my code, at the top of an else statement:
do
{
$id = rand(1000000, 9999999);
if (!Util::in_array_r($id, $db->query_all('SELECT * FROM tickets')))
{
break;
}
}
while (true);
$emailsubject = $db->escape($emailsubject);
$emailbody = $db->escape($emailbody);
$from = $db->escape($from);
$db->query("INSERT INTO tickets VALUES ($id, '$emailsubject', '$emailbody', '$from')");
foreach ($config['staff_emails'] as $email)
{
mail($email, "[$id] [NEW TICKET] $emailsubject", $emailbody, "Reply-to: $to");
}
I don't understand why it is doing this - it never used to, and the only thing I changed was making all the SQL queries work :/
Does anyone know why this is happening? Could the for loop be running endlessly? The database contains only one row though, so I don't see how it could,
Thanks, Liam
Upvotes: 0
Views: 49
Reputation: 2609
You have an infinite loop. You probably want to do
while ($row = mysqli_fetch_array($this->query($query)) {
$res[] = $row;
instead of
for ($res = array(); $tmp = mysqli_fetch_array($this->query($query));) $res[] = $tmp;
Upvotes: 1