user5801835
user5801835

Reputation:

Why I get Allowed memory size of 134217728 bytes exhausted error

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 />

Upvotes: 0

Views: 725

Answers (2)

iainn
iainn

Reputation: 17417

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)) {
  ...
}

Upvotes: 2

Nutshell
Nutshell

Reputation: 8537

Your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop.

See this post on SO : Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

Check if your while() is not an infinite loop ;)

Upvotes: 0

Related Questions