0
    $store_list = array();
    $this->db->select('user_time');
    $this->db->from('users');
    $this->db->where('staff_id',$userid);
    $result = $this->db->get();

    $result= $result->result();
    foreach($result as $rows){
        array_push($store_list, $rows["user_time"]);
    }
    return $store_list;

I have written a function in a model which contains the above-mentioned code which fetches user's time when user's id passed as a parameter and returns user's time it to the controller function.

But I'm getting the following error: Fatal Error: Cannot use object of type stdClass as array

What's causing this error? Any ideas?

Note: the sql query returns all matched records. It can be one or more than one records.

2 Answers 2

1

may be $result it is object not array, use like this

array_push($store_list, $rows->user_time);
Sign up to request clarification or add additional context in comments.

2 Comments

User this $result = $this->db->get()->result_array();
also put a check before using foreach loop like this if ($result) { foreach..... }
1

The error is because

$result= $result->result();

here $result is an Std Class object, to access its element use -> instead of [] like:

array_push($store_list, $rows->user_time);

Or to use to use it like an array:

$result = $this->db->get()->result_array();

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.