dra87370 2018-11-03 18:28 采纳率: 100%
浏览 50
已采纳

php递归函数没有返回任何值

I write this recursive function but its not working. Not able to find the mistake. Its not returning anything. But If I write an "echo" on else statement its printing perfectly.

function find_parent($referralid,$leg)
{   
    $query="SELECT memberid FROM memberdetails where parentid='".$referralid."'  and leg='".$leg."'";
    $result=mysql_query($query);
    if (mysql_num_rows($result) == 0) 
        return $referralid;
    else
    {
        while($row = mysql_fetch_array($result))
        {
            find_parent($row[0],$leg);
        }
    }   
}
  • 写回答

1条回答 默认 最新

  • dsj83686 2018-11-03 18:33
    关注

    When you have recursion, you need to pass the value back as well, otherwise the value isn't propagated back up. So the line which is...

    find_parent($row[0],$leg);
    

    needs to be

    return find_parent($row[0],$leg);
    

    This is assuming that you will only ever get 1 row as a parent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了