Welcome Guest, Not a member yet? Register   Sign In
Object error
#1

[eluser]stuffradio[/eluser]
For the life of me I can't figure out what's wrong.

Here is the code:
Code:
function artists()
  {
$data['logged_in'] = $this->loginlib->checkSession();
$data['error'] = "You're not an artist!";
$info = $this->users->getInfo();

    if (!$this->loginlib->checkSession())
    {
      $this->login();
    } else {

    $this->load->view('head', $data);
     if ($info->group <= 2)
    {
    $this->load->view('artists', $data);
    } else {
    $this->load->view('error', $data);
    }
    }
  }
Here is the error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/home.php

Line Number: 72

Line in question: if ($info->group <= 2)
#2

[eluser]steelaz[/eluser]
Try a var_dump($info) before line 72 to figure out what's missing
#3

[eluser]stuffradio[/eluser]
It says

array(0) { }
#4

[eluser]Scriptor[/eluser]
The problem's most likely in the getInfo() function of the users model. Are any queries working right, are you getting any results from those queries?
#5

[eluser]stuffradio[/eluser]
Yes I am,

here is the code:
Code:
&lt;?php

class Users extends Model
{
var $username;

  function Users()
  {
    parent::Model();
  }

  function listAll()
  {
    $query = $this->db->get('users');
    return $query->result();
  }

  function getInfo()
  {
    if ($this->username)
    {
    $get = $this->db->get_where('users', array('username' => $this->username));
    return $get->row();
    } else {
      return false;
    }
  }

}
#6

[eluser]Scriptor[/eluser]
Check the contents of $this->username before it's used in the query.
#7

[eluser]Colin Williams[/eluser]
Any time getInfo() returns FALSE, you're going to get that error. You might just have to extend that check.

Code:
if ($info && $info->group <= 2)
#8

[eluser]stuffradio[/eluser]
Thanks for that Colin.

Is that some sort of bug in PHP or CI?
#9

[eluser]xwero[/eluser]
It's a bug in your code Smile

You are doing a check for a user group before you even know if there is a user. If you do something like this
Code:
function artists()
{
   $data['logged_in'] = $this->loginlib->checkSession();
   $data['error'] = "You're not an artist!";
   $info = $this->users->getInfo();

   if (! $info)
   {
       $this->login();
   } else {
       $this->load->view('head', $data);
       if ($info->group <= 2)
       {
          $this->load->view('artists', $data);
       } else {
          $this->load->view('error', $data);
       }
   }
}
You know the $info variable is going to have data. If for some reason you checkSession method returns true but the username isn't set in the model you have the bug you are experiencing.
#10

[eluser]Colin Williams[/eluser]
Right. If $info is FALSE (which your model function is indeed capable of returning) and you later try to access a property of $info (which is false, which is a bool, and not an object) it's not gonna work.




Theme © iAndrew 2016 - Forum software by © MyBB