Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Please friends, I need help for my code.

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

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

$id = $search->searchParent(230);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to bobble the return value of your function.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...