CodeIgniter problem retrieving and displaying data

2019-05-26 17:00发布

问题:

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?

回答1:

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



回答2:

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

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

Refer.



回答3:

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.



回答4:

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



回答5:

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



回答6:

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.



回答7:

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



标签: codeigniter