43

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.

sea_1987
  • 2,722
  • 12
  • 41
  • 68

7 Answers7

47

Most likely, the userdata() function is returning an object, not a string. Look into the documentation (or var_dump the return value) to find out which value you need to use.

Pekka
  • 418,526
  • 129
  • 929
  • 1,058
  • 2
    @sea look what properties the object has e.g. using var_dump(), and select the approriate one e.g. `$CI->session->userdata('user_id')->username` ("username" being an example, I have no idea which properties exist in your case) – Pekka Aug 31 '10 at 09:15
  • `var_dump($var)` did it. – George Oct 25 '17 at 12:14
9

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

Widor
  • 12,075
  • 6
  • 35
  • 60
roshan
  • 91
  • 1
  • 1
5

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

Nɪsʜᴀɴᴛʜ ॐ
  • 2,126
  • 3
  • 22
  • 43
2

try this

return $query->result_array();
Roopchand
  • 2,160
  • 2
  • 15
  • 20
2

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.

treeface
  • 12,934
  • 3
  • 48
  • 57
  • The weird thing is that return $query->row() comes from a prewritten script, it his not his creation. Check http://www.nelsonwells.net/blog/2010/05/10/creating-a-simple-extensible-codeigniter-authentication-library/ – stef Sep 01 '10 at 07:55
1

Like others said, echo command is not able to print out an object on its own. For that reason, You can consider using print_r to display the content of your object.

So, in your case:

 print_r($query);
0

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.

Vicky Salunkhe
  • 4,955
  • 2
  • 30
  • 42