php Codeigniter:在非对象上调用成员函数 result_array()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18347028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter: Call to a member function result_array() on a non-object
提问by Ethan
I'm using Codeigniter to build a webapp and I received this error:
我正在使用 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
This is the people.php class:
这是 people.php 类:
public function __construct()
{
parent::__construct();
}
function People() {
}
function view($id) {
echo "Hello world " . $id;
}
function all() {
$this->load->model('people_model', '', TRUE);
$allContacts = $this->people_model->get_all_contacts();
$results = array();
foreach ($allContacts->result_array() as $row) {
$eachrow = array("personID"=>$row['personID'],"fName"=>$row['fName'],"lName"=>$row['lName'],"phoneNumber"=>"",
"emailAddress"=>$row['emailAddress'],"address"=>$row['address'],"city"=>$row['city'],"state"=>$row['state'],"zip"=>$row['zip']);
$results = push_array($results, $eachrow);
}
foreach ($results as $row) {
$phoneData = $this->people_model->get_phone_by_id($row['personID']);
$phoneNumbers = "";
foreach ($phoneData->result_array() as $row2) {
$phoneNumbers = $row2['type'] . " " . $row2['phoneNumber'] . " " . $row2['extension'];
}
$results['phoneNumber'] = $phoneNumbers;
}
$data['title']="Page Title Goes Here";
$data['username']="Your Username";
$this->load->view('header', $data);
$this->load->view('people_results', $results);
$this->load->view('footer');
}
}
Here is the people_model class:
这是 people_model 类:
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query->result();
}
function get_phone_by_id($id) {
$query = $this->db->query('SELECT * FROM phoneNumber WHERE personID = '.$id);
return $query->result();
}
}
The only thing I might question in database.php is if the hostname needs a port number, but I don't think that helped when I tried it.
我在 database.php 中唯一可能会质疑的是主机名是否需要端口号,但我认为这在我尝试时没有帮助。
I just tried adding (based on similar question in side bar)
我只是尝试添加(基于侧栏中的类似问题)
$this->mydb = $this->load->database('realDB', TRUE);
to the people_model and changing the db to mydb and received this error:
到 people_model 并将数据库更改为 mydb 并收到此错误:
You have specified an invalid database connection group.
and this is my line in database.php:
这是我在 database.php 中的一行:
$db['default']['database'] = 'realDB';
Thanks for all your help.
感谢你的帮助。
采纳答案by Rooster
since get_all_contacts is already using the result() function in your model, you can't also use the result_array() function in your controller. result_array() would be a method of the $query object. The quick and dirty way to get this working(which might break other stuff if its also using the get_all_contacts method) would be to change your get all contacts function to the following:
由于 get_all_contacts 已经在您的模型中使用了 result() 函数,因此您也不能在控制器中使用 result_array() 函数。result_array() 将是 $query 对象的一个方法。使这个工作快速而肮脏的方法(如果它也使用 get_all_contacts 方法可能会破坏其他东西)是将你的 get all contacts 函数更改为以下内容:
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query;
}
however, if you want to be smarter about it and not risk breaking other stuff, you can pass a param from the controller, and only return query if its set like so:
然而,如果你想更聪明一点,而不是冒着破坏其他东西的风险,你可以从控制器传递一个参数,并且只在设置如下时才返回查询:
REVISED CONTROLLER LINE**
修改控制器线**
$allContacts = $this->people_model->get_all_contacts(true);
REVISED MODEL CODE
修订后的模型代码
function get_all_contacts($special = false) {
$query = $this->db->query('SELECT * FROM person');
if($special)
{
return $query;
}
return $query->result();
}