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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵