致命错误:调用非对象中的函数

时间:2014-04-28 20:07:16

标签: php sql function

我打印出表格中的信息(tblgroups)。之后我想打印出来自团队成员的头像(来自tblusers)(tblgroup_member)

当我尝试将group_id(从我打印出来的组中)推送到Getmembers函数时,它告诉我我在非对象上调用函数( $memberinfo = $group->Getmembers($group["group_id"]);)。但我宣布$group = new Group();

它告诉我错误在这一行:$memberinfo = $group->Getmembers($group["group_id"]);

PHP

$group = new Group();
                foreach ($groups as $group) {



                        echo "<p  class='groupname'><a href='group.php?group_id=" . $group['group_id']. "'>" . $group['group_name']  . "</a></p>"; 
                        echo $group['group_id'];

                        $memberinfo = $group->Getmembers($group["group_id"]);
                    /*  
                        foreach ($memberinfo as $member)
                        {
                            echo "<p>" . $member['avatar'] . "</p>";
                        }

                        */
                }

FUNCTION GETMEMBERS(如有必要)

public function Getmembers($group_id) {
        $db = new Db();

            $select = "SELECT

                            g.group_id,
                            m.user_id,
                            u.avatar

                        FROM ((tblgroups g INNER JOIN tblgroup_member m ON g.group__id = m.group__id) INNER JOIN tblusers u ON u.user_id = m.user_id)
                        WHERE g.group_id = " .  $group_id . " ";



        $result = $db -> conn -> query($select);
        $result_array=array();
        while ($row = mysqli_fetch_array($result)) {
             $result_array[]=$row;                                                                                          
        }
        return $result_array;

    }

1 个答案:

答案 0 :(得分:0)

你有第一件事

$group = new Group();

然后循环

foreach ($groups as $group) {

你正在使用的循环

$memberinfo = $group->Getmembers($group["group_id"]);

此处$group->并不意味着该对象是来自foreach的数组数据。或者换句话说,它被覆盖并且$group不再是一个对象

<强>解决方案

更改

$group = new Group();

$group_obj = new Group();

然后

$memberinfo = $group->Getmembers($group["group_id"]);

$memberinfo = $group_obj->Getmembers($group["group_id"]);