Php 递归函数不工作

Php 递归函数不工作,php,function,recursion,Php,Function,Recursion,这是功能不工作的原因,是正确的: function primaryParent($id) { $result = $this->querySingle('select parentid from menu where id = ' . $id . ' limit 1'); if ($result) $result = $this->primaryParent($result); else return $result; }

这是功能不工作的原因,是正确的:

function primaryParent($id)
{
    $result = $this->querySingle('select parentid from menu where id = ' . $id . ' limit 1');
    if ($result) 
        $result = $this->primaryParent($result);
    else 
        return $result;
}
我的错误是什么

此函数必须返回主父id,而不是第一个父id

在“数据库项”组的“按parrentid”字段中,然后显示:

  • 主菜单项
    • 子菜单项1
    • 子菜单项2
      • 子菜单项1
      • 子菜单项2
      • 子菜单项3
    • 子菜单项3
我需要从子菜单项获取主菜单项id


我使用SQLite3函数。

对于
if($result)
案例,您的函数不会返回任何内容。

您可能需要
返回$this->primarypenter($result)

当然可以,但是如果我使用
返回$this->primaryParent($result)
if($result)
的情况下,我看到了这个错误:
AH00052:child pid 23153退出信号分段错误(11)
和Nginx show
502坏网关
,这非常罕见,显然在底层PHP代码中发现了一些错误。