2015-12-16 24 views
1

我面臨着以下錯誤:錯誤試圖讓非對象和未定義變量的性質:笨

Trying to get property of non-object and Undefined variable php errors in my code

控制器:

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 ?> 

我已經使用此方法顯示數據庫中的信息,但現在看不到錯誤。

回答

1

result()回報

This method returns the query result as an array of objects, or an empty array on failure

所以,你需要獲取單個數據形成使用->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循環

,讓您的數據數據庫For exm

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

函數getDoctorId在函數getDoctorInformation中被調用,我試過但仍然是相同的錯誤。 –

+0

使用我的更新後的代碼?也波西完整的錯誤消息 – Saty

+0

是使用後,更新後的我得到這個錯誤: 一個PHP錯誤遇到 嚴重性:注意 消息:未定義的變量:doctorinfo 文件名:觀點/ patient_msgview.php 行號:100 回溯: 文件:/home/a15_web01/web/application/views/patient_msgview.php 線:100 功能:_error_handler 文件:/首頁/ a15_web01 /網絡/ applicati上/控制器/ PatientController.php 線:51 功能:解析 文件:/home/a15_web01/web/index.php 線:292 功能:require_once –