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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?