0

I am getting a memory error when running a PHP function.

The error shows as...

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4294967296 bytes)

The PHP script is...

function getPageContent($page) {

    //Open a new connection to the MySQL server
    $mysqli = new mysqli('localhost','MY_USER','MY_PASS','MY_DATABASE');
    $account = MY_ACCOUNT_NUMBER;
    $project = 1;

    if($stmt=$mysqli->prepare("SELECT content FROM pages WHERE account_id=? AND id=? LIMIT 1")){

        $stmt-> bind_param("ii", $account, $page);

        // Execute
        $stmt-> execute();

        // Bind results
        $stmt-> bind_result($content);

        // Fetch value
        while ( $stmt-> fetch() ) {
          echo $content;
    }

    // Close statement
    $stmt-> close();
    $mysqli-> close();
    }

}

It is saying the error has to do with the bind_result function in the PHP code. I am stuck on this. I tried increasing my memory limit in PHP.ini but that did not work and I should not be taking up that much memory anyways.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Mike Barbaro
  • 245
  • 1
  • 17
  • Either it's due to my state of tiredness, but I doubt that this function is causing the issue - at least I can't see an obvious mistake here. Are you executing anything else in a loop which may run endlessly? – maio290 Jun 04 '20 at 23:22
  • I have another almost identical function to get the page's title but that's all I can think of. – Mike Barbaro Jun 04 '20 at 23:30
  • please take a look at `memory_get_peak_usage()` and `memory_get_usage()` from the manual https://www.php.net/manual/de/function.memory-get-peak-usage.php Put in the script that calls the function and then compare with the allocated memory to see if there is concordance –  Jun 04 '20 at 23:42
  • I did and it doesn't seem to give me any clues. The PHP function is still trying to use 4294967296 bytes which just doesn't make sense why it would try to use that much memory. – Mike Barbaro Jun 04 '20 at 23:48
  • When this error occurs, usually something runs indefinitely. Thus you can just put a break; into each loop one by one and determine which loop is causing the issue. If a break in your while loop fixes the problem, something might be off here, but I just don't spot it now. – maio290 Jun 04 '20 at 23:52
  • @maio290 I just tried that too but same error. It says the error is in the bind_param function line. – Mike Barbaro Jun 05 '20 at 00:18
  • Does this answer your question? [PHP mysql\_stmt::fetch() gives PHP Fatal error memory exhausted](https://stackoverflow.com/questions/17509531/php-mysql-stmtfetch-gives-php-fatal-error-memory-exhausted) – Dharman Jun 05 '20 at 09:50

1 Answers1

3

I figured it out! You must store the result before bind_param...

$stmt-> store_result();
Mike Barbaro
  • 245
  • 1
  • 17