2

Here is my function. It is very simple.

function load_data() {

        $query = $this->db->query('SELECT * FROM configurations WHERE username="' . $this->session->userdata('username') . '"');
        return $query;
   }

My controller has this line of code:

$data['query'] = $this->configurations->load_data();

In my view, I tried:

foreach($query->result_array() as $row) {

echo $row->first;

}

But I get an error that I am trying to get a property of a non-object. Isn't the query being returned from the model as an object?

2
  • This is a re-post of this question: stackoverflow.com/questions/4708994/codeigniter-syntax Commented Jan 18, 2011 at 0:17
  • Please don't ask the same question over again. You can edit your question, leave comments on answers, etc. I've merged your previous question into this one. Commented Jan 18, 2011 at 14:38

7 Answers 7

5

You should use $query->result_array, row_array, result, or row otherwise your returning the object intead get the results. Check the CI manual.

Sign up to request clarification or add additional context in comments.

Comments

2

You are returning the results as array and using $row as object!
Try:

foreach($query->result() as $row) {

Refer.

Comments

1

Try changing $this->load->model('login/configurations', '', TRUE); to $this->load->model('login/configurations', 'configurations', TRUE); and see if it works. If it does, it is related to how you're extending your model class. That is, it would be related to what name you give inside configurations.php.

Hope this helps.

1 Comment

Try returning $query->result_array() as Sein Oxygen suggested. I think he's on to something. :-)
1

Your undefined variable error tells me that your query might not be running correctly. To diagnose...enable the profiler to check your query.

From the documentation:

$this->output->enable_profiler();

Permits you to enable/disable the Profiler, which will display benchmark and other data at the bottom of your pages for debugging and optimization purposes.

To enable the profiler place the following function anywhere within your Controller functions: $this->output->enable_profiler(TRUE);

When enabled a report will be generated and inserted at the bottom of your pages.

Your query will be shown at the end of the page

Double check your query syntax to make sure it is running properly, and

Comments

0

your code in it's current state is returning an object of objects and arrays:

print_r($query)

CI_DB_mysql_result Object
(
    [conn_id] => Resource id #29
    [result_id] => Resource id #39
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 3
    [row_data] => 
)

you need to access the individual properties to get to the actual data.

$result=$query->result();
print_r($result);

should do it

Comments

0

Had this issue before - basic problem is that if the Query returns no result set then $query->result_array() == NULL

NULL is not a list and can't be processed in a foreach. I have found that checking for this condition solves this issue.

2 Comments

That didn't work. I tried: if ($query->result_array() != NULL) { foreach($query->result_array() as $row) { echo $row->first; } } Other thoughts?
I checked my code (as @Sein above suggests) I return the array and not the object from LoadData(). I voted for his answer.
0

From your question, you are getting the result as a pure array (result_array) but printing the data as object ($row->first).

result() or row() is returning in object but result_array is returning in array.

change your view part like,

  foreach($query->result_array() as $row) {

     echo $row['first'];

   }

or if you want to use an object then,

foreach($query->result() as $row) {

     echo $row->first;

   }

Generating Query Results in Codeigniter

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.