CodeIgniter Forums
Error when getting value from query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Error when getting value from query (/thread-31174.html)



Error when getting value from query - El Forum - 06-09-2010

[eluser]bondjp[/eluser]
Hi, i'm getting an error when trying to display some info i have from a query.
The error is this: Trying to get property of non-object
I've spent several hours on this but i can't see where i'm wrong.

Print_r shows something like this:
Array ( [0] => stdClass Object ( [id] => 10 [user_id] => 10 [username] => James ))

Model
Code:
$query = $this->db->get('user_profiles');
$usr=$query->result();
foreach($usr as $row){
    $users[]=$row;
}
return $users;

View

Code:
<?php foreach($results as $row):
    $username=$results->username;
?>



Error when getting value from query - El Forum - 06-09-2010

[eluser]n0xie[/eluser]
Code:
// Model
$query = $this->db->get('user_profiles');
if ($query->num_rows() >0)
{
  return $query->result();
}
else
{
  return FALSE;
}

// View
foreach ($results as $row):
  echo $row->username;
endforeach;



Error when getting value from query - El Forum - 06-09-2010

[eluser]packetfox[/eluser]
You could do:

Code:
$query=$this->db->get('user_profiles');
$results = $query->result_array();
foreach($results as $result){
print "$result[username]<br />";
}



Error when getting value from query - El Forum - 06-09-2010

[eluser]bondjp[/eluser]
[quote author="n0xie" date="1276101453"]
Code:
// Model
$query = $this->db->get('user_profiles');
if ($query->num_rows() >0)
{
  return $query->result();
}
else
{
  return FALSE;
}

// View
foreach ($results as $row):
  echo $row->username;
endforeach;
[/quote]


That did it.
Thanks.