1

In one of my new codeigniter project, one of my colleague wrote a helper method array_to_object so that he can call the variables in views as $row->field instead of $row['field'].

I thought codeigniter default behaviour allows us to retrieve data from database in $row->field (as object). Can anyone pls enlight me data flow in codeigniter?

1 Answer 1

3

Codeigniter supports both an array and an object oriented style to retrieve data from DB so both of these styles are equal(from the user guide):

oop style

$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result() as $row)
{
    echo $row->title;
    echo $row->name;
    echo $row->email;
}

array style

$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result_array() as $row)
{
    echo $row['title'];
    echo $row['name'];
    echo $row['email'];
}

here is the user guide: http://codeigniter.com/user_guide/database/examples.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.