dou11655853 2013-08-21 00:29
浏览 51
已采纳

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

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

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');
}
}

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();
}
}

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.

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:

You have specified an invalid database connection group.

and this is my line in database.php:

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

Thanks for all your help.

  • 写回答

1条回答 默认 最新

  • duan35557593 2013-08-21 01:25
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:第一张图是发送数据代码,第二第三张图是接收数据并进行DAC转换的函数,若用串口一接收数据并进行DAC转换,主函数代码该怎么写
  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面