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
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
Source: Stackoverflow.com