1

I'm getting "Page not available" if I run the following code:

namespace Database;
class Table extends \Database\Connection {
    // ...

    /**
     * Execute query and return result
     * @param type $query
     * @param type $sqlWildcards
     * @return SplFixedArray ResultSet
     */
    public static function query($query, $sqlWildcards = array()) {
        // Do some query stuff
        // $stmt is a PHP PDO Statement

        // Additional result manipulation
        $rowCount = 0;
        $rsStorage = new \SplFixedArray(500000);
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $modRow = static::modifyRow($row);
            if ($modRow !== false) {
                $rsStorage[$rowCount] = $modRow;
                $rowCount++;
            }
        }

        // Resize 
        $rsStorage->setSize($rowCount);
    }
}

Method modifyRow($row):

/**
 * Gives the possibility to modify a result for child classes 
 * @param array $row
 * @return array
 */
protected static function modifyRow($row) {
    return $row;
}

But if I do the following:

    while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
        //$modRow = static::modifyRow($row);
        //if ($modRow !== false) {
            $rsStorage[$rowCount] = $modRow;
            $rowCount++;
        //}
    }

Everything works fine!

Edit: The test above makes no sense, I forgot to set $modRow to $row - this fails as well.

Another hint: if I do something like this

    while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
        $modRow = static::modifyRow($row);
        if ($modRow !== false) {
            $rsStorage[$rowCount] = $modRow;
            $rowCount++;
        }

        // Works
        if($rowCount == 5000) { break; }

        // Crashes (Page not available
        if($rowCount == 10000) { break; }
    }

Error reporting is activated but there's no internal server error, though. Just "page not available"

Edit: 9742 is the magic number. When $rowCount hits 9743 it crashes - doesn't matter if I test it from 0 to 9743 or 9744 to x

Edit2: If I use array instead of SplFixedArray everything's fine. Does not make sense since splfixedarray is more memory efficient than array()

Edit3: I wrote the memory usage into a file until the script crashes, it reaches a peak of 54MB - server limit is 128MB

Edit4: Apache log shows me a 'zend_mm_heap corrupted'

Never had this before, also google could not help me Any idea?

kair
  • 946
  • 1
  • 10
  • 16
  • This code is inside a class right? – OIS Jun 30 '15 at 13:49
  • Yes, its kind of a PHP PDO wrapper for additional stuff – kair Jun 30 '15 at 13:52
  • Your "everything works" actually adds null (?) to the array, not the row. And if 5000 works but not 10000 then it looks like a memory problem? – OIS Jun 30 '15 at 13:55
  • but a memory problem would throw an internal server error, wouldn't it? I'm getting "page not available" and why does it work if I do not use the placeholder method "modifyRow"? – kair Jun 30 '15 at 13:55
  • In your example which works $modRow is perhaps undefined (depending on code above). Even if it has a value it always adds the same value in memory to the array. – OIS Jun 30 '15 at 13:58
  • but how can it be undefined if I pass a PHP PDO ResultSet row (with fetch_assoc) to a method that just simply returns $row? – kair Jun 30 '15 at 13:59
  • Your code which "works" has commented out that part. Add a dump and exit after 5 iterations of the loops for simple debugging. – OIS Jun 30 '15 at 14:01
  • I tested it with 5 example results, printing it before and after modifyRow, it's 1:1 the same. – kair Jun 30 '15 at 14:07
  • You are not reading my messages... Try with your "working" example which commented out this part: //$modRow = static::modifyRow($row); - but kept this the same: $rsStorage[$rowCount] = $modRow; – OIS Jun 30 '15 at 14:08
  • I dont see where `$stmt` is defined, and is that all :modifyRow code does – meda Jun 30 '15 at 15:14
  • It is defined as described in my code comments. It's not part of the issue that's why I have not included it. The query is fine, the results are fine as well - I will provide some test data tomorrow. – kair Jun 30 '15 at 16:56
  • I added 2 edits to my post. First of all: my working test does not work since I forgot to change $modRow to $row - also I found out when everything crashes (second edit) – kair Jul 01 '15 at 08:25
  • I tested array() instead of SplFixedArray(int $size) to check memory issues and it works. But this does not make sense to me, SplFixedArray is much more efficient than array(), both should not work.. – kair Jul 01 '15 at 08:29
  • I added a memory peak test - it reaches 54MB until it crashes, server limit is 128MB – kair Jul 01 '15 at 08:51
  • There ought to be an error log with a productive error message somewhere. The "not available" response your web server is fronting is not useful in debugging this. Figure out where your error logs are. – deceze Jul 01 '15 at 10:10
  • Apache throws 'zend_mm_heap corrupted' - I searched for a solution and found http://stackoverflow.com/questions/2247977/what-does-zend-mm-heap-corrupted-mean - I tried to increase the output_buffer but this does not work for me – kair Jul 15 '15 at 07:50

0 Answers0