Smokey
Smokey

Reputation: 1897

php- Fatal Error: Cannot use object of type stdClass as array

    $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.

Upvotes: 0

Views: 3207

Answers (2)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26288

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();

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34924

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

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

Upvotes: 1

Related Questions