tolutronics
tolutronics

Reputation: 21

Allowed memory size of 2147483648 bytes exhausted

Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 20480 bytes) in line 52 (the error still occur even if i set memory_limit to infinity)

$query= mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM users WHERE email='[email protected]'"));
    while($row = $query){
      $data[] = array(
        'username'=> $row['username'], // Line 52
        'email'  => $row['email'],
        'phone'=> $row['phone'],
        'city'=> $row['city'],
        'gender'=> $row['gender'],
      );

    }
    if($query) $result = json_encode(array('success'=>true, 'data'=>$data));
    else $result = json_encode(array('success'=>false, 'msg'=>mysqli_error($conn)));

    echo $result;

  }

Upvotes: 1

Views: 2011

Answers (1)

Dharman
Dharman

Reputation: 33237

Don't use while loop. You have just created an infinite loop, because you assign one variable to another every time you loop without changing the condition. You don't need a loop at all, but if you do want a loop for some reason then you need to use foreach.

$query = $conn->query("SELECT * FROM users WHERE email='[email protected]'");
$data = [];
foreach ($query as $row) {
    $data[] = [
        'username' => $row['username'],
        'email' => $row['email'],
        'phone' => $row['phone'],
        'city' => $row['city'],
        'gender' => $row['gender'],
    ];
}
if ($data) {
    $result = json_encode(['success' => true, 'data' => $data]);
} else {
    $result = json_encode(['success' => false, 'msg' => 'No data available!']);
}

echo $result;

You don't need a loop in your code at all. Just fetch everything in one go.

$query = $conn->query("SELECT * FROM users WHERE email='[email protected]'");
$data = $query->fetch_all(MYSQLI_ASSOC);
if ($data) {
    $result = json_encode(['success' => true, 'data' => $data]);
} else {
    $result = json_encode(['success' => false, 'msg' => 'No data available!']);
}

echo $result;

Upvotes: 1

Related Questions