gklaxman
gklaxman

Reputation: 165

trying to get property of non-object codeigniter

I am calling a function twice from two different functions, But for one of the function it says "trying to get property of non-object" first function is

$sql=$this->db->query("select * from gl_events where entry_id='$entry_id'");
if($sql->num_rows() > 0)
{
    $row3=$sql->row();
    $event_respond[] = 
        array(
            'entry_id'=>$row3->entry_id,
            'event_title'=>$row3->event_title,
            'location'=>$row3->event_loc_add_ln1,
            'location1'=>$row3->event_loc_add_ln2,
            'start'=>$row3->event_start_date,
            'start1'=>$row3->event_start_time,
            'created'=>$row3->user_id,
            'about'=>$row3->event_descr,
            'end'=>$row3->event_end_time,
            'end1' => $row3->event_end_time,
            'userid'=>$row3->user_id,
             //here the function goes to gl_get_att function and  
             //calls gl_getusername function,
            'attend'=>$this->gl_get_att($row3->entry_id),
             // here it calls gl_getusername function directly
            'username'=>$this->gl_getusername($row3->user_id)
       );
}

gl_get_att definition:

public function gl_get_att($entry_id)
{
    $query=$this->db->query("select * from gl_event_participants where event_id='$entry_id'");
    //echo "select * from gl_event_participants where event_id='$entry_id'";
    $row1=$query->row();
    foreach($query->result() as $row)
    {
        $data[]= array(
            'user_id'=>$row->user_id,
            'username'=>$this->gl_getusername($row->user_id)// here it calls get username function
        );
    }
    //print_r($data[1]);
    //print_r($data[0]);
    return $data;
}

gl_getusername definition:

public function  gl_getusername($user_id)
{
    $query=$this->db->query("select user_name from gl_user where user_id = '$user_id'");
    $row1=$query->row();
    //print_r($row1->user_name);
    return $row1->user_name; //This is line 396
}

Now for gl_get_att its returning the proper values but for the first function where i have called it directly its not working

error message: A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: events/gl_event_model.php
Line Number: 396

line 396 is marked above

This was working fine on saturday after the weekend its not working !!! strange

Upvotes: 1

Views: 8855

Answers (1)

Doug Kress
Doug Kress

Reputation: 3537

You aren't checking to see if $query is valid, or if $query->row() returns an object. This error would be generated if the passed in $entity_id is not found in the database.

Upvotes: 3

Related Questions