简体   繁体   English

尝试获取非对象和未定义变量的属性时出错:Codeigniter

[英]Error Trying to get property of non-object and Undefined variable: Codeigniter

I am facing the following error: 我遇到以下错误:

Trying to get property of non-object and Undefined variable php errors in my code 试图在我的代码中获取非对象和未定义变量php错误的属性

Controller: 控制器:

function showDoctorInformation(){
    $this->load->model('PatientModel');
    $data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
    $this->parser->parse('patient_msgview', $data);

}

Model: 模型:

function getDoctorId() {
        $this->db->from('person');
        $this->db->select('doctorId');
        $doctorId = $this->db->get()->result();
        return $doctorId;
    }

function getDoctorInformation() {


    $doctorId = $this->getDoctorId();

        $this->db->from('DoctorInfo');
        $this->db->where('doctorId', $doctorId);
        $this->db->select('name', 'surname', 'Bio', 'Address', 'img');
        $doctorinfo = $this->db->get()->result();
        return $doctorinfo;

}

View: 视图:

<?= $doctorinfo->name ?>

I have displayed information from the database before with this method and I can't see the error now. 使用此方法之前,我已经显示了数据库中的信息,现在看不到错误。

result() return result()返回

This method returns the query result as an array of objects, or an empty array on failure 此方法以对象数组或失败时为空数组的形式返回查询结果

So you need to fetch single data form your database using ->row() 因此,您需要使用->row()数据库中获取单个数据

function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
     $row=$query->row();// fetch single row
     return $row->doctorId;// get doctor id
} else {
    return FALSE;
}
}

And in viwe you have to get your data using foreach loop 而且,您必须使用foreach循环获取数据

For exm 对于exm

foreach ($doctorinfo as $row)
{
        echo $row['title'];
        echo $row['name'];
        echo $row['body'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM