1

I ran into a problem with CodeIgniter when trying to call an object from data stored in a session.

Code:

public function do_index_1 ()
{   
    $tags= $this->input->post('tags');  
    $data['recommendation_for_you_unlimited']=$this->db->query
        ("SELECT *, MATCH(tags, message) AGAINST ('$tags') as score 
        FROM game
        WHERE MATCH(tags, message) AGAINST ('$tags')
        ORDER BY score
        "); 
    $data['recommendation_num'] = $this->db->get('game')->num_rows();
    $this->session->set_userdata($data);
}

public function do_index_3 ()
{
    $data['recommendation_num'] = $this->session->userdata('recommendation_num');
    $data['recommendation_for_you_unlimited'] = $this->session->userdata('recommendation_for_you_unlimited');
}

The data is stored into the session variable in do_index_1 and pulled to show in do_index_3. The first variable $data['recommendation_num'] is output fine, but the second $data['recommendation_for_you_unlimited'] triggers the error:

The script tried to execute a method or access a property of an incomplete object.

I have searched here and there and found that it might be because $data['recommendation_for_you_unlimited'] is an object. But I'm not sure how to fix it.

1 Answer 1

1

$this->db->query() returns an object (the specific object depends on which of CodeIgniter's database drivers you use). I think you mean to use $this->db->query()->result() which returns an array of rows as a result of the query.

Use the following, instead:

$data['recommendation_for_you_unlimited'] = $this->db->query
    ("SELECT *, MATCH(tags, message) AGAINST ('$tags') as score 
    FROM game
    WHERE MATCH(tags, message) AGAINST ('$tags')
    ORDER BY score")->result();

The error you are getting is a result of trying to store a class in userdata. When you store something using $this->session->set_userdata(), the data passed is serialized into a string. When it's retrieved later using $this->session->userdata(), it's unserialized back into the original data. With certain datatypes, such as classes, you end up losing things that cannot be serialized properly.

My recommendation is to stay away from storing classes or objects in userdata (or PHP's $_SESSION, because it does the same thing). Though, you can store simple objects that just have properties, without a problem.

In this case, it just looks like you're missing ->result().

Here is documentation on generating query results in CodeIgniter

Here is documentation for both CodeIgniter's Active Record and its Session class:

Here is some basic documentation for how $_SESSION works

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.