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);
        }
    }   
}

图片转代码服务由CSDN问答提供 功能建议

我写这个递归函数但它不起作用。 无法找到错误。 它没有返回任何东西。 但是如果我在else语句上写一个“echo”就可以完美地打印出来。

  function find_parent($ referralid,$ leg)
 {
 $ query =“SELECT memberid FROM  memberdetails其中parentid ='“。$ referralid。”'和leg ='“。$ leg。”'“; 
 $ result = mysql_query($ query); 
 if(mysql_num_rows($ result)== 0)\  n 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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题