当由另一个函数处理时,结果从PHP类查询返回错误

时间:2010-09-24 05:28:08

标签: php class mysqli

以下作品:

$user_list = new user_list();
$all_users_list = $user_list->getAllUsers();

以下不起作用,我不确定为什么不这样做:

$user_list = new user_list();

以上回报:

 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given on line 59

供参考的课程:

class dbo extends mysqli {

    public function __construct(){
        require('config_db.inc.php');
        parent::__construct($db_host, $db_user, $db_pass, $db_name);

        if (mysqli_connect_error()) {
            die("Connect Error: (".mysqli_connect_errno().") - ".mysqli_connect_error());
        }
    }



}

class user_list extends user {

    var $table_name = "cms_users";  

    function __construct($group = "") {
        if ($group == "") {
            return $this->getAllUsers();
        } else {
            $this->getUsersFromGroup($group);
            return $this->result;
        }
    }   

    function getAllUsers() {
        $dbh = new dbo();
        $sql = "SELECT * FROM {$this->table_name}";
        return $dbh->query($sql);
    }

    function getUsersFromGroup($group) {
        $dbh = new dbo();
        $sql = "SELECT * FROM {$this->table_name} WHERE group=$group";
        return $dbh->query($sql);
    }


}

1 个答案:

答案 0 :(得分:1)

问题是您为不同目的重用变量名$user_list

$user_list = new user_list();
// $user_list is now an object

$user_list = $user_list->getAllUsers();
// $user_list is now a mysqli resource

...

// line 59: code that expects $user_list to be a mysqli resource

比较:

$user_list = new user_list();
// $user_list is now an object

...

// line 59: code that expects $user_list to be a mysqli resource

您无法从构造函数返回任何内容。 new关键字将始终为您提供一个对象,从构造函数中返回值无处可去。

if ($group = "")中也有错误,您的意思是if ($group == "")