Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3 bytes) after ini_set

At first, I've already looked at this, this and this.

I've received the following error:

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

I'm working with php 5.4and sql Anywhere 11.

The solution to this is according to this is putting ini_set('memory_set',-1); in my php-file, but after doing this I get another error:

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

EDIT: my code is

<?php
    ini_set('memory_set',-1);
    $connect = sasql_connect("UID=username;PWD=pass");
    echo "Connection succeed";

    $result = sasql_query($connect, "SELECT * FROM table1, table2");

    if(!$result){
    echo "sasql_query failed";
    return 0;
    } else {
    echo "query completed successfully\n";
    sasql_result_all($result);
    }
    sasql_close($conn);
?>

I hope someone can help me.

SOLUTION: I've found the solution: I've added a WHERE (columnName1 = columnName2), split the result and it works again, relative fast!

<?php
    $connect = sasql_connect("UID=username;PWD=pass");
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
    $results_page = 100;
    $start_from = ($page-1) * $results_page + 1;    
    $result = sasql_query($connect, "SELECT TOP $results_page START AT $start  * 
                    FROM table1, table2 WHERE (columnName1 = columnName2)");

    if(!$result){
    echo "sasql_query failed";
    return 0;
    } else {
    echo "query completed successfully\n";
    sasql_result_all($result);
    }
    sasql_close($conn);
?>

Ofcourse I add in my php-page a <a href="<?php echo $page -1; ?>">Previous</a> and a <a href="<?php echo $page + 1; ?>">Next</a>

Thanks to you all for the help!

like image 742
Kenny Avatar asked Dec 05 '22 09:12

Kenny


1 Answers

You should use

ini_set("memory_limit",-1);

and not "memory_set". Also, look out for this: https://stackoverflow.com/a/5263981/2729140 (Suhosin extension has it's own memory limit setting).

If your script needs a lot of memory, then you should try and revise it. One thing to be aware of are unlimited SQL queries.

Your tables can have A LOT of records, so it's wise to always limit your queries. If you need to fetch all the records from the table, then you should do it by pages, using LIMIT ... OFFSET SQL constructs.

like image 151
sp-niemand Avatar answered Dec 24 '22 01:12

sp-niemand