从函数到另一个函数调用veriable和object

时间:2018-05-29 12:08:25

标签: php function pdo

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

   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 个答案:

没有答案