Reputation: 2954
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.
Upvotes: 59
Views: 515710
Reputation: 2168
In some cases the error message is misleading.
In my case I was providing my where clause with wrong type of data.
$this->db->where('channel_reports.channel_id', $channelId)
In my case, I thought $channelId
is an integer, but after a quick debug, I realized that $channelId
is an array. I fixed the problem, and made sure that I am providing where
method with correct inputs.
So, in these cases, CI should display a proper error message.
How this answer is helping?
So many people like me, may face this error just because they provided where
method with wrong type of data and since the error message in these circumstances is misleading they might spend a lot of time on different things to debug their code. So, before anything else, make sure you are providing correct type of data for your where clause.
Upvotes: 0
Reputation: 143
If you are having this problem that means your array is in a type in which you can not convert it to string type. Your object
is not in string
type.
But, you can convert it like this:
$data= json_decode( json_encode($data), true);
If you want more reference you can go here.
Hope it helps.
Upvotes: 3
Reputation: 203
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);
Upvotes: 9
Reputation: 11025
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.
Upvotes: 1
Reputation: 2914
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
Upvotes: 8
Reputation: 91
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
Upvotes: 9
Reputation: 13351
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.
Upvotes: 2
Reputation: 449783
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.
Upvotes: 53