Welcome Guest, Not a member yet? Register   Sign In
Error on simple query and display
#1

[eluser]huggiebear[/eluser]
I get the following error when trying to display the results of a query (query returns a single row).

Quote:Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\ci\coresystem\libraries\Loader.php(647) : eval()'d code on line 9

Here's the code in the controller:

Code:
<?php

class Newsletter extends Controller {

   function index()
   {
      $data['title'] = 'My Database Test';
      $data['query'] = $this->db->get('main_issues');
      $this->load->view('display_newsletter', $data);
   }
}
?>

and here's the code in the view:

Code:
<html>
<head>
  <title><?=$title?></title>
</head>
<body>
  <h1>Database Test</h1>
  <ul>
  &lt;?php foreach($query->result() as $row):?&gt;
  <li>&lt;?=$row['issue_name']?&gt;</li>
  &lt;?php endforeach;?&gt;
  </ul>
&lt;/body&gt;
&lt;/html&gt;

A var_dump() of $data in the view gives this:

Code:
Array
(
    [view] => display_newsletter
    [vars] => Array
        (
            [title] => My Database Test
            [query] => CI_DB_mysql_result Object
                (
                    [conn_id] => Resource id #24
                    [result_id] => Resource id #25
                    [result_array] => Array
                        (
                        )

                    [result_object] => Array
                        (
                        )

                    [current_row] => 0
                    [num_rows] => 1
                )

        )

    [return] =>
)

Does anyone have any ideas?

Regards
Huggie
#2

[eluser]xwero[/eluser]
the get method returns an object not an array, you have to do something like this
Code:
$query = $this->db->get('main_issues');
$data['query'] = $query->result_array();
#3

[eluser]huggiebear[/eluser]
Thanks for the advice. I just altered this line in the view:
Code:
&lt;?php foreach($query->result() as $row):?&gt;
to this
Code:
&lt;?php foreach($query->result_array() as $row):?&gt;
Regards
Huggie
#4

[eluser]xwero[/eluser]
[quote author="huggiebear" date="1188486132"]Thanks for the advice. I just altered this line in the view:
Code:
&lt;?php foreach($query->result() as $row):?&gt;
to this
Code:
&lt;?php foreach($query->result_array() as $row):?&gt;
Regards
Huggie[/quote]

If you changed your controller code to get the query as an array you can't use it as an object anymore

in the view you have to do

Code:
&lt;?php foreach($query as $row):?&gt;

If you keep using it as an object you have to change your

Code:
&lt;?php $row['var']:?&gt;

to

Code:
&lt;?php $row->var:?&gt;

That was the initial problem.
#5

[eluser]almarjin[/eluser]
I have learned from this thread.
My problem was also solved.

Cheers!!!
#6

[eluser]Sumon[/eluser]
almarjin From my point of view, a very good approach will be using the following standard

Code:
$query = $this->db
           ->select("u.email, u.country_id as country, c.country as country_name")
           ->from('users as u')
           ->join('countries as c', 'u.country_id = c.id', 'left')
           ->where('u.id', $id)
           ->get();
if ($query->num_rows() > 0)    
{
  $rows = $query->row(); // Access using $row->field_name; (Single Row)
  //$rows = $query->row_array(); // Access using $row['field_name'] (Single Row);
  //$rows = $query->record_array(); // Access using $row['field_name'] (Multiple Row);
  return $rows;
}
else
return false;
One further point is that, try to use model as much as possible. Whatever the application seems to tiny or small. It have a huge advantage what later you realize easily.
You can use same query for single and multiple results. Just a matter of change in $query->row_array() or $query->record_array(). hope you also like it.
So my suggestion is always try to use model.
#7

[eluser]almarjin[/eluser]
Thanks Sumon. I like that code it's clear and easy to understand. I will use that code.
#8

[eluser]Shane Robinson[/eluser]
@Sumon: Thanks for such a clear example and code!

I'm just getting started with CI and your example was exactly what I needed help with.
#9

[eluser]gregorius[/eluser]
Thank you,
solve my problem.
#10

[eluser]mobilizedesign[/eluser]
does

$this->db->query("SELECT * FROM example");

return results as an array or as an object?

edit:

foreach ( $objects->result() as $object )
{
echo $object['name'];
}

BUZZZ!!! giant fail here.

correct syntax ...
foreach ( $objects->result() as $object )
{
echo $object->name;
}
... since i'm using an object method of looping through results

hope this helps someone




Theme © iAndrew 2016 - Forum software by © MyBB