代码之家  ›  专栏  ›  技术社区  ›  Second View

将veriable和object从函数调用到另一个函数[重复]

  •  0
  • Second View  · 技术社区  · 6 年前

    我有这个函数来获取用户及其工作的根。

       public function getRootUser()
    {
        $query = "SELECT id, nameOfPerson, parent FROM person WHERE parent is null";
        $statment = $this->db->prepare($query);
        $statment->execute();
        echo '<ul id ="family">';
        while($family = $statment->fetch(PDO::FETCH_OBJ)){
    
            echo '<li>'. $family->nameOfPerson;
          }
    }
    

    我有这个函数来得到根的子元素,但它不起作用为什么?

    public function getChildOfParentUser($parentId)
    {
    
            $parentId = $family->id;
            $query1 = "SELECT id, nameOfPerson, parent FROM person WHERE id = $parentId";
            $statment1 = $this->db->prepare($query1);
            $statment1->bindValue('id', $family->id);
            $statment1->execute();
            if($statment1->rowCount() > 0){
            echo '<ul>';
            while($family2 = $statment1->fetch(PDO::FETCH_OBJ)){
                echo '<li>' . $family2->nameOfPerson . '</li>';
            }
                echo '</ul>';
            }
                echo '</li>';
    
                echo '</ul>';
    }
    

    他发出了这个通知

    Notice: Undefined variable: family in /var/www/html/Family.php on line 59
    
    Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 59
    
    Notice: Undefined variable: family in /var/www/html/Family.php on line 62
    
    Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 62
    

    这是我桌子上的一些数据

    id  nameOfPerson    parent
    3   John             NULL
    4   Michel            3
    5   Husam             4
    6   Khalaf            5
    7   Mark              5
    ----------------------------
    
    0 回复  |  直到 6 年前