[php] Object of class stdClass could not be converted to string

I am having a problem with PHP at the moment, I am getting this error,

Object of class stdClass could not be converted to string the error occurs when I run this portion of code in my site,

function myaccount() {
    $data['user_data'] = $this->auth->get_userdata($this->uri->segment(3));
    //var_dump($data['user_data']);
    $this->load->model('users_model');
    $data['user_info'] = $this->users_model->get_user_and_roadmaps_by_id($this->uri->segment(3));
    $this->template->build('users/my_account', $data);
}

The line in which the error is occuring is this one,

$data['user_data'] = $this->auth->get_userdata($this->uri->segment(3));

And this line is calling this function,

   function get_userdata() {

        $CI =& get_instance();

        if(!$this->logged_in()) {
            return false;
        } else {
            $query = $CI->db->get_where('users', array('user_id' => $CI->session->userdata('user_id')));
            return $query->row();
        }
    }

I don't really now what this problem is, so any help would be great.

This question is related to php codeigniter object stdclass

The answer is


What I was looking for is a way to fetch the data

so I used this $data = $this->db->get('table_name')->result_array();

and then fetched my data just as you operate on array objects.

$data[0]['field_name']

No need to worry about type casting or anything just straight to the point.

So it worked for me.


I use codeignator and I got the error:

Object of class stdClass could not be converted to string.

for this post I get my result

I use in my model section

$query = $this->db->get('user', 10);
        return $query->result();

and from this post I use

$query = $this->db->get('user', 10);
        return $query->row();

and I solved my problem


You mentioned in another comment that you aren't expecting your get_userdata() function to return an stdClass object? If that is the case, you should mind this line in that function:

return $query->row();

Here, the CodeIgniter database object "$query" has its row() method called which returns an stdClass. Alternately, you could run row_array() which returns the same data in array form.

Anyway, I strongly suspect that this isn't the root cause of the problem. Can you give us some more details, perhaps? Play around with some things and let us know how it goes. We can't play with your code, so it's hard to say exactly what's going on.


try this

return $query->result_array();

In General to get rid of

Object of class stdClass could not be converted to string.

try to use echo '<pre>'; print_r($sql_query); for my SQL Query got the result as

stdClass Object
(
    [num_rows] => 1
    [row] => Array
        (
            [option_id] => 2
            [type] => select
            [sort_order] => 0
        )

    [rows] => Array
        (
            [0] => Array
                (
                    [option_id] => 2
                    [type] => select
                    [sort_order] => 0
                )

        )

)

In order to acces there are different methods E.g.: num_rows, row, rows

echo $query2->row['option_id'];

Will give the result as 2


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor

Examples related to stdclass

How to access a property of an object (stdClass Object) member/element of an array? How to convert an array into an object using stdClass() PHP Foreach Arrays and objects Object of class stdClass could not be converted to string Convert/cast an stdClass object to another class PHP: Count a stdClass object What is stdClass in PHP?