开发者

PHP function does not return a value

I have a recursive function that returns a value.

    public function getparent ($user)
    {
        $referrer=''; $pos=0;$this->setFieldNames();
        $result=mysql_query("select * FROM ". $this->dbtablename ." WHERE ".$this->id_field . " 开发者_如何学Go= '$user'", $this->dbConnectionID);
        while($row=mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $referrer=$row['referrer'];
            $export=array();
            foreach($this->tablefields as $val){
                $export[$val] = $row[$val];
            }
        }
        if($referrer != "0" && $referrer != "")
        {
            $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->id_field . " ='$referrer'", $this->dbConnectionID);
            while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
            {
                $export=array();
                foreach($this->tablefields as $val){
                    $export[$val] = $row2[$val];
                }
            }
        }

        $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->parent_id_field . " ='$referrer' order by id asc", $this->dbConnectionID);
        while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
        {
            $usernames[]=$row2['username'];
        }
        $pos=array_search($user, $usernames);
        if( $referrer == '0' || $pos >=1 )
        {echo 'xxxxxxx';
            return $export;
        }
        else
        {
            $this->getparent($referrer);
        }
    }

It seems that when the $referrer is zero it doesnt return any value, yet it echoes 'xxxxxxx'. What can the problem be!?


There's a problem with:

    while($row=mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $referrer=$row['referrer'];
        $export=array();
        foreach($this->tablefields as $val){
            $export[$val] = $row[$val];
        }
    }

You're replacing your export array on every iteration - is that your intent? The data is being thrown away for all but the last row. Then you do it again in this block:

    if($referrer != "0" && $referrer != "")
    {
        $result2=mysql_query("select * from " . $this->dbtablename . " where " . $this->id_field . " ='$referrer'", $this->dbConnectionID);
        while($row2=mysql_fetch_array($result2, MYSQL_ASSOC))
        {
            $export=array();
            foreach($this->tablefields as $val){
                $export[$val] = $row2[$val];
            }
        }
    }

Where you're (a) only getting the last row of data and (b) overriding the data from the first block (perhaps that latter is desired).

And finally if you ever hit this block (referrer isn't zero the first time through your code):

    else
    {
        $this->getparent($referrer);
    }

You never reutrn a value from the recursive call, it should be:

    else
    {
        return $this->getparent($referrer);
    }

Which I suspect is the actual issue. But I've got to question whether this recursive approach is best, rather than an iterative one.


Try to declare $export before the while loop

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜