phpcodeigniterquery-builderresultset

CodeIgniter's row() is leading to error: Trying to get property of non-object


I have a CodeIgniter script which returns rows from different tables.

Unfortunately, it gives me the error: Trying to get property of non-object.

This is my code. Where is a problem?

$this->db->select('*');
$this->db->from('orders');
$this->db->where('order_id',$order_id);
$array_keys_values = $this->db->get();
$row = $array_keys_values->row();

$this->db->select('*');
$this->db->from('pacients');
$this->db->where('pacient_account_id',$row->order_pacient_id);
$array_keys_values2 = $this->db->get();
$row2 = $array_keys_values2->row();

$this->db->select('*');
$this->db->from('doctors');
$this->db->where('doctor_account_id',$doctor_id);
$array_keys_values3 = $this->db->get();
$row3 = $array_keys_values3->row();

Solution

  • Try

    $this->db->select('*');
    $this->db->from('orders');
    $this->db->where('order_id',$order_id);
    $array_keys_values = $this->db->get();    
    if ($array_keys_values->num_rows() > 0) {
        foreach ($array_keys_values->result() as $row) {
           // now you can work with $row
        }
    }
    
    $this->db->select('*');
    $this->db->from('pacients');
    $this->db->where('pacient_account_id',$row->order_pacient_id);
    $array_keys_values2 = $this->db->get();
    if ($array_keys_values2->num_rows() > 0) {
        foreach ($array_keys_values2->result() as $row2) {
            // now you can work with $row2
        }
    }
    
    $this->db->select('*');
    $this->db->from('doctors');
    $this->db->where('doctor_account_id',$doctor_id);
    $array_keys_values3 = $this->db->get();
    if ($array_keys_values3->num_rows() > 0) {
        foreach ($array_keys_values3->result() as $row3) {
            // now you can work with $row3
        }
    }