PHP递归类返回NULL [英] PHP Recursive class returning NULL

查看:54
本文介绍了PHP递归类返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拜托朋友,我的代码需要帮助.

Please friends, I need help for my code.

我的 searchParent 函数在多次执行 self :: 时总是返回NULL

My searchParent function always returns NULL when it executes the self:: more than once

class Search {
    public function listParents() {
        $sql = "SELECT id FROM parents";
        return pg_query($sql);
    }

    public function searchParent($id) {
        $parents= $this->listParents($con);
        while ($r = pg_fetch_array($parent)){
            if ($id === $r['id_parent']){
                return $id;
            } 
        }
        $sql = "SELECT id_parent FROM parent WHERE id_parent = $id";
        $query = pg_query($sql);
        $result = pg_fetch_array($query);

        self::searchParent($result['id_parent']);
    }
}

我的电话,假设父表中有数字230

My call, assuming there is the number 230 in the parent table

$id = $search->searchParent(230);

推荐答案

您需要弄虚函数的 return 值.

在调用 self :: searchParent 之前,应添加return语句:

Before calling the self::searchParent you should add return statement:

return self::searchParent($result['id_parent']);

这篇关于PHP递归类返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆