Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 43624344 bytes)

3

I get this error when I go through a for with the results of a sql:

    $datos = serialize($res);
    $html = "";
        for($i = 0; $i < count($res);$i++){                     
            $html = $html.'<form action="exportarTxartelak.php" method="post">
                <div class="row alumno">
                    <div class="col-4">'
                        .$res[$i]["NOMBRE"].'
                    </div>
                    <div class="col-8" style="/*margin-bottom: 2rem; padding-left: 5px">                        
                            <input type="submit" name="word" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["WORD"].'">
                            <input type="submit" name="pdf" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["PDF"].'">
                            <input type="submit" name="mail" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["EMAIL"].'">
                    </div>              
                </div>              
                <div class="col-12 info">
                        <textarea style="/*display: none;" name="resultado">'.base64_encode($datos).'</textarea>
                        <input  style="/*display: none;" type="text" name="USCOD" value="'.$res[$i]["USCOD"].'">
                </div>      
            </form>';       
        }
        echo $html;

There comes a point where this for returns error, exactly on lap # 19.

The complete for can occupy up to 1041 results. I intend to create a list (without pagination) that the user shows me with 3 buttons.

What leaves me with concern is that I have made loops with these same data but more complex (generating a complete html) and I have not given any problem.

Finally, if I remove the base64_encode($datos) , it lists me correctly, but I need it inside the form.

Note: I can not access the server files to edit the memory

    
asked by Lombarda Arda 22.05.2017 в 15:52
source

3 answers

4

The problem you are suffering because of this line:

<textarea style="/*display: none;" name="resultado">'.base64_encode($datos).'</textarea>

For each record that you will show on the screen you are entering in <textarea> the content of $res COMPLETE, and not that particular result.

By putting the HTML into a variable BEFORE sending it to the browser, it increases so much that it exceeds the PHP memory limit. Uploading the maximum PHP size will only delay the problem a bit, it does not solve it.

You have the option to correct it with the serialize within the loop or to give direct output to the HTML without going through an intermediate variable, or both (as I propose at the end).

The solution I propose:

$datos = serialize($res);
for($i = 0; $i < count($res); $i++) {                     
  echo '<form action="exportarTxartelak.php" method="post">
            <div class="row alumno">
                <div class="col-4">'
                    .$res[$i]["NOMBRE"].'
                </div>
                <div class="col-8" style="/*margin-bottom: 2rem; padding-left: 5px">                        
                        <input type="submit" name="word" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["WORD"].'">
                        <input type="submit" name="pdf" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["PDF"].'">
                        <input type="submit" name="mail" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["EMAIL"].'">
                </div>              
            </div>              
            <div class="col-12 info">
                    <textarea style="/*display: none;" name="resultado">'.base64_encode($datos).'</textarea>
                    <input  style="/*display: none;" type="text" name="USCOD" value="'.$res[$i]["USCOD"].'">
            </div>      
        </form>';       
    }

And if I'm right and you just want the serialized data of the current record to appear, then you should do:

for($i = 0; $i < count($res); $i++) {                     
  $datos = serialize($res[$i]);
  echo '<form action="exportarTxartelak.php" method="post">
            <div class="row alumno">
                <div class="col-4">'
                    .$res[$i]["NOMBRE"].'
                </div>
                <div class="col-8" style="/*margin-bottom: 2rem; padding-left: 5px">                        
                        <input type="submit" name="word" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["WORD"].'">
                        <input type="submit" name="pdf" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["PDF"].'">
                        <input type="submit" name="mail" style="margin: 0 0 0 10px" class="btn btn-primary" value="'.$lang["EMAIL"].'">
                </div>              
            </div>              
            <div class="col-12 info">
                    <textarea style="/*display: none;" name="resultado">'.base64_encode($datos).'</textarea>
                    <input  style="/*display: none;" type="text" name="USCOD" value="'.$res[$i]["USCOD"].'">
            </div>      
        </form>';       
    }
    
answered by 22.05.2017 / 16:18
source
0

It happens because you no longer have assigned memory in the hosting you use. They give you 128 mb and you already exhausted them. There are several ways to fix that.

I would first release $ data or dollars because what I see keeps the same but serialized. You are using double the memory for something that may be the same.

On the other hand, if you have access, you can modify the php.ini of the server to assign more memory. The field is memory_limit

You can also do it in the .httaccess by adding php_value memory_limit 256M.

The temporary solution is to put more memory in that file ini_set ('memory_limit', '256M');

    
answered by 22.05.2017 в 16:00
0

You have to correct the php.ini

memory_limit=512M

Then restart the apache and you're done

    
answered by 22.05.2017 в 16:16