Why did I have an allowable memory size of 134217728 bytes error exhausted

advertisements

I just can not find any error at my code. the problem coming with the array_push method (Allowed memory size of 134217728 bytes exhausted).

Code:

<?php
    require "dbConnect.php";
    $username = $_POST['username'];

    if ($username != '') {
        $sql = "SELECT * FROM users WHERE username='$username'";

        $result = mysqli_query($con, $sql);
        $check = mysqli_fetch_array($result);
        $data = array();

        if (isset($check)) {
            echo "4";
            while ($row = mysqli_fetch_array($result)){
                echo "2";
                array_push ($data, array('name'=>$row[1], 'username'=>$row[2], 'password'=>$row[3], 'email'=>$row[4]));
            }

            echo json_encode(array("response"=>$data));
            mysqli_close($con);

        } else {
            echo "0";
        }
    } else {
        echo "3";
    }
?>

Error:

<br />
<b>Fatal error</b>:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) in <b>/home/u766232015/public_html/phpScripts/getUSerData.php</b> on line <b>14</b><br />


That while() loop isn't going to ever stop looping, nothing is being changed inside it. You need to move the mysqli_fetch_array call to the loop condition, e.g.

while ($row = mysqli_fetch_array($result)) {
  ...
}