开发者

error in running recursion

开发者 https://www.devze.com 2023-01-03 22:27 出处:网络
if running function returns server misconfiguration error function build_path($cid) { $result = array();

if running function returns server misconfiguration error

    function build_path($cid)
    {
        $result = array();
        $DB = new MySQLTable;
        $DB->TblName = 'shop_categories';
        $where['cat_id']['='] = $DB->CleanQuest($cid);
        $res = $DB->Select('cat_id,cat_name,cat_parent', $where);
        if($res !== 'false')
        {
            $pid = mysql_fetch_array($res);
            if($pid['cat_parent'] !== 0)
            {
                Echo $pi开发者_JS百科d['cat_parent'];
                build_path($pid['cat_parent']);
            } else {
                Echo $pid['cat_id'];
                return true;
            }
        }
        return false;
    }

I can't find an error here. Please help.

Sorry for disturbing you all. The trouble was in comparison 'if($pid['cat_parent'] !== 0)': $pid['cat_parent'] was a string with int(0)

Can i build this function to store full path without using GLOBALS and SESSION vars?


Things to check for:

  1. Is cat_parent ever null?
  2. Are there any recursively parented pairs (A is parent of B, B is parent of A; A->B, B->C, C->A)?
  3. Are there any cases where the row is self parented (A is parent of A)

Try making this modification (for debugging):

function build_path($cid) {
    static $done = array();
    if (isset($done[$cid])) {
        throw new Exception('We have already looked up CID: '.$cid);
    } else {
        $done[$cid] = true;
    }
    //.......
}

That will let you tell if you've got a recursive loop going on.

Also, set error_reporting to max, and check your error log. My guess is that it's either a stack overflow (your recursion is going too deep) or a memory limit issue (that it uses up too much memory and is killed)...

0

精彩评论

暂无评论...
验证码 换一张
取 消