0

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

2 Answers2

2

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)) {
  ...
}
iainn
  • 15,089
  • 9
  • 27
  • 37
  • Thanks, the problem solved but now there is another one. I get back from the server an empty JSONObject ({"result":[]}). Maybe you know why? –  Apr 06 '16 at 13:57
0

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 ;)

Community
  • 1
  • 1
Vincent G
  • 8,178
  • 1
  • 16
  • 31
  • Thanks that was the issue –  Apr 06 '16 at 13:57
  • Answer to your question below : can you do a `print_r($data);` just before the `json_encode()` ? – Vincent G Apr 06 '16 at 15:09
  • Actually the program don't get into the loop –  Apr 06 '16 at 15:16
  • `$result = mysqli_query($con, $sql);` => where is `$con` ? – Vincent G Apr 06 '16 at 15:22
  • There is no problem with $con because I know that the program can find the user name in my table (The program can get in the 'if( isset($check))'). The problem comes with the loop. –  Apr 06 '16 at 15:28
  • Can you update your code with the solution of @iainn ? Maybe try some `print_r($row);` and tell us what it returns – Vincent G Apr 06 '16 at 15:51
  • I updated the post, so now I can explain it better. It print 4 but not print 2 so you can understand that the condition: '$row = mysqli_fetch_array($result)' is true from the start even when I have rows at my table (users)... –  Apr 06 '16 at 16:08
  • How about when you comment the `$check` var and the `if(isset($check){ ... }` condition ? – Vincent G Apr 06 '16 at 16:14
  • Check the code, I already did it. That is work and return me 4 (that situation) –  Apr 06 '16 at 16:26
  • No, I was meaning **remove** theses lines just to see – Vincent G Apr 06 '16 at 16:28
  • I did not understand what you meant –  Apr 06 '16 at 16:31