Patrik Dendis
Patrik Dendis

Reputation: 313

Codeigniter get row

I use codeigniter for my website, but I have a problem I have done script who returns rows from different tables. But it write me 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();

Upvotes: 0

Views: 706

Answers (2)

broswilli
broswilli

Reputation: 317

No row was returned. Use an if statement with a num_rows() > 0 to check

Upvotes: 0

stealthyninja
stealthyninja

Reputation: 10371

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

Upvotes: 2

Related Questions