Codeigniter:调用非对象上的成员函数result_array() [英] Codeigniter: Call to a member function result_array() on a non-object
问题描述
我使用Codeigniter构建webapp,我收到此错误:
I'm using Codeigniter to build a webapp and I received this error:
Fatal error: Call to a member function result_array() on a
non-object in /var/www/application/controllers/people.php on line 29
这是people.php类:
This is the people.php class:
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');
}
}
这里是people_model类:
Here is the people_model class:
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();
}
}
我可能在database.php中提出的唯一问题是如果主机名需要端口号,但我不认为这有助于我尝试。
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.
我刚刚尝试添加(基于类似的问题在侧栏) / p>
I just tried adding (based on similar question in side bar)
$this->mydb = $this->load->database('realDB', TRUE);
添加到people_model并将db更改为mydb并收到此错误:
to the people_model and changing the db to mydb and received this error:
You have specified an invalid database connection group.
这是我在database.php中的行:
and this is my line in database.php:
$db['default']['database'] = 'realDB';
推荐答案
< >解决方案
因为get_all_contacts已经在你的模型中使用了result()函数,你不能在你的控制器中使用result_array()函数。 result_array()将是$ query对象的一个方法。快速和脏的方式来获得这个工作(如果它也使用get_all_contacts方法可能会打破其他的东西)将更改您的获取所有联系人函数到以下:
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:
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();
}
这篇关于Codeigniter:调用非对象上的成员函数result_array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!