0

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();
3
  • Do you have db_debug enabled in your database configuration? Seems like the $this->db->get() returns false and that might mean the generated query is invalid (no such table/column) or such. Commented Oct 28, 2012 at 15:42
  • Yes I have db_debug set on TRUE. But my query is ok. Commented Oct 28, 2012 at 15:54
  • The reason you're getting that error is because the result is returned as a two-dimensional array and not an object. See my answer below. Commented Oct 28, 2012 at 23:06

3 Answers 3

2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

row() returns null when there are no rows in the result set. This is likely what is breaking your script.

If all three queries actually related, then perhaps us a LEFT JOIN to make just one trip to the database. Use aliases in the select if multiple tables use the exact same column name.

return $this->db
    ->select('
        o.order_id,
        o.name order_name,
        p.pacient_account_id,
        p.name pacient_name,
        d.doctor_account_id,
        d.name doctor_name
    ')
    ->from('orders o')
    ->join('pacients p', 'p.pacient_account_id = o.order_pacient_id', 'left')
    ->join('doctors d', 'd.doctor_account_id = o.order_doctor_id', 'left')
    ->where('o.order_id', $order_id)
    ->get()
    ->row();

This will return a flat object with 6 properties or null.

Comments

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.