Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:对非对象调用成员函数result_array()_Php_Codeigniter - Fatal编程技术网

Php Codeigniter:对非对象调用成员函数result_array()

Php Codeigniter:对非对象调用成员函数result_array(),php,codeigniter,Php,Codeigniter,我正在使用Codeigniter构建Web应用程序,收到以下错误: Fatal error: Call to a member function result_array() on a non-object in /var/www/application/controllers/people.php on line 29 You have specified an invalid database connection group. 这是people.php类: 以下是people_模型

我正在使用Codeigniter构建Web应用程序,收到以下错误:

Fatal error: Call to a member function result_array() on a 
non-object in /var/www/application/controllers/people.php on line 29
You have specified an invalid database connection group.
这是people.php类:

以下是people_模型类:

在database.php中,我可能会问的唯一一个问题是主机名是否需要端口号,但我认为在我尝试时这并没有帮助

我刚刚尝试添加(基于侧栏中的类似问题)

将数据库更改为mydb并收到此错误:

Fatal error: Call to a member function result_array() on a 
non-object in /var/www/application/controllers/people.php on line 29
You have specified an invalid database connection group.
这是我在database.php中的行:

$db['default']['database'] = 'realDB';

感谢您的帮助。

由于get\u all\u contacts已在您的模型中使用result()函数,因此您不能在控制器中也使用result\u array()函数。结果_array()将是$query对象的一个方法。要使此功能正常运行(如果同时使用get_all_contacts方法,可能会破坏其他功能),最快捷的方法是将get all contacts函数更改为以下内容:

function get_all_contacts() {
    $query = $this->db->query('SELECT * FROM person');
    return $query;
}
但是,如果您想更聪明一些,并且不想冒险破坏其他东西,您可以从控制器传递一个参数,并且只有在查询设置为这样时才返回查询:

修订的控制器行**

$allContacts = $this->people_model->get_all_contacts(true);
修订后的型号代码

function get_all_contacts($special = false) {
    $query = $this->db->query('SELECT * FROM person');
    if($special)
    {
        return $query;
    }
    return $query->result();
}
function get_all_contacts($special = false) {
    $query = $this->db->query('SELECT * FROM person');
    if($special)
    {
        return $query;
    }
    return $query->result();
}