1

I am facing the following error:

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

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.

1 Answer 1

1

result() return

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

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

For exm

foreach ($doctorinfo as $row)
{
        echo $row['title'];
        echo $row['name'];
        echo $row['body'];
}
Sign up to request clarification or add additional context in comments.

6 Comments

the function getDoctorId is onlly called in the function getDoctorInformation, i have tried but still the same error.
Yes after using your updated i get this error: A PHP Error was encountered Severity: Notice Message: Undefined variable: doctorinfo Filename: views/patient_msgview.php Line Number: 100 Backtrace: File: /home/a15_web01/web/application/views/patient_msgview.php Line: 100 Function: _error_handler File: /home/a15_web01/web/application/controllers/PatientController.php Line: 51 Function: parse File: /home/a15_web01/web/index.php Line: 292 Function: require_once
Post your controller code too and value of print_r($doctorinfo);
thats is my controller: ** function showDoctorInformation(){ $this->load->model('PatientModel'); $data['doctorinfo'] = $this->PatientModel->getDoctorInformation(); $this->parser->parse('patient_msgview', $data); }** and the value i dont see anything when i use print_r($doctorinfo);
use echo $this->db->last_query(); to print last query and check it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.