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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services