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


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

我面临以下错误:

尝试获取非对象和未定义变量php的属性我的代码中的错误

控制器:

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

型号:

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

视图:

<?= $doctorinfo->name ?>

我以前用这个方法显示过数据库中的信息,现在看不到错误。

result()返回

此方法以对象数组或故障时的空阵列

因此,您需要使用->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;
}
}

在viwe中,您必须使用foreach循环获取数据

对于exm

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