MySQL recursive function call eats too much memory and dies

I have the following recursive function that works ... down to the point. The script then requests more memory when requests exceed about 100, and when I add more memory, the script usually just dies (I have a white screen running out in my browser).

public function returnPArray($parent=0,$depth=0,$orderBy = 'showOrder ASC'){



    $query = mysql_query("SELECT *, UNIX_TIMESTAMP(lastDate) AS whenTime 
        FROM these_pages 
        WHERE parent = '".$parent."' AND deleted = 'N' ORDER BY ".$orderBy."");

    $rows = mysql_num_rows($query);

    while($row = mysql_fetch_assoc($query)){

        // This uses my class and places the content in an array.
        MyClass::$_navArray[] = array(

            'id' => $row['id'], 
            'parent' => $row['parent']

        );

        MyClass::returnPArray($row['id'],($depth+1));   

    }
    $i++;

}

Can someone help me make this request less resource intensive? Or find a way to free memory between calls ... somehow.

+3
source share
7 answers

, , -. , parent_id - ? AND id != '".(int)$parent."' where, ...

** : :

while($row = mysql_fetch_assoc($query)){
    if (isset(MyClass::$_navArray[$row['id']])) continue;

    MyClass::$_navArray[$row['id']] = array(
        'id' => $row['id'], 
        'parent' => $row['parent']
    );
    MyClass::returnPArray($row['id'],($depth+1));
}
+1

- (, , 0)? , , returnPArray.

0

... ? , - , ? , tress db, , , db.

, , , db.

, , , , /. DB /.

0

, . , , 100 , , 100 /. , fetch ( false). - , , .

, , , while() . , mysql_free_result(), .

, resulset .

0

? , , , , . , , . , parent_id (, , ), , .

, , , :

public function returnPArray($orderBy = 'showOrder ASC'){
    $query = mysql_query("SELECT *, UNIX_TIMESTAMP(lastDate) AS whenTime 
        FROM these_pages 
        WHERE deleted = 'N' ORDER BY parent ASC,".$orderBy."");

    $rows = mysql_num_rows($query);

    while($row = mysql_fetch_assoc($query)){

        // This uses my class and places the content in an array.
        MyClass::$_navArray[] = array(

            'id' => $row['id'], 
            'parent' => $row['parent']

        );
    }
}
0

PHP:

$nodeList = array();
$tree     = array();

$query = mysql_query("SELECT *, UNIX_TIMESTAMP(lastDate) AS whenTime 
    FROM these_pages WHERE deleted = 'N' ORDER BY ".$orderBy."");
while($row = mysql_fetch_assoc($query)){
    $nodeList[$row['id']] = array_merge($row, array('children' => array()));
}
mysql_free_result($query);

foreach ($nodeList as $nodeId => &$node) {
    if (!$node['parent_id'] || !array_key_exists($node['parent_id'], $nodeList)) {
        $tree[] = &$node;
    } else {
        $nodeList[$node['parent_id']]['children'][] = &$node;
    }
}
unset($node);
unset($nodeList);

.

0

.

  • . , ini_set ('memory_limit', -1).
  • , , , script , display_errors, error_reporting E_NONE. , set_time_limit (0).
  • "" "" . , , .

№ 3 - , .

, , " ".

, , . , Doctrine ORM, .

0

Source: https://habr.com/ru/post/1747575/


All Articles