Welcome Guest, Not a member yet? Register   Sign In
Error: Trying to get property of non-object [SOLVED]
#1

[eluser]the jan[/eluser]
Im trying to get a simple value from a field within my table but I always get the error "Trying to get property of non-object"... what am I missing?

Code:
function getNotifyNum()
        {
            if($this->uri->segment(3) != "") {
                $user_data['verify_string'] = $this->uri->segment(3);
            }
            else
            {
                $user_data['verify_string'] = $this->input->post('verify_code');
            }
            
            $this->db->where('verify_string',$user_data['verify_string']);
            $query = $this->db->get('submitters');
            $user_row = $query->row();

            $this->db->select('id,user_id,rand_id');
            $this->db->where('user_id',$user_row->id);
            $query_num = $this->db->get('submitted',1);
            $num_row = $query_num->row();
            
            return $num_row->rand_id;
            
        }

The error is produced by the line with "return $num_row->rand_id;" even though I'm selecting that field and it exists in the database. I also already checked with count_all_results that there definitely is a result returned.

Is the underscrore in the field name a problem? Any ideas?

Thanks!
#2

[eluser]xwero[/eluser]
the underscore isn't the problem, but i have no idea what could be wrong. the code seems fine to me, but i would use a join instead of two separate queries.
#3

[eluser]the jan[/eluser]
I solved the problem. It was actually caused by a bug in another function which writes the user_id into the database. That function added the wrong user_id to the database, so I couldn't link the id of the user table entry to the other table.

In addition I followed your suggestions and used a join.

Code:
function getNotifyNum()
        {
            if($this->uri->segment(3) != "") {
                $user_data['verify_string'] = $this->uri->segment(3);
            }
            else
            {
                $user_data['verify_string'] = $this->input->post('verify_code');
            }
            
            $this->db->select('submitted.rand_id');
            $this->db->from('submitted');
            $this->db->join('submitters','submitters.id = submitted.user_id');
            $this->db->where('submitters.verify_string',$user_data['verify_string']);
            $query = $this->db->get();
            $entry = $query->row();
            
            return $entry->rand_id;
            
        }




Theme © iAndrew 2016 - Forum software by © MyBB