关于php:类stdClass的对象无法转换为字符串

Object of class stdClass could not be converted to string

我目前在使用PHP时遇到问题,遇到此错误,

Object of class stdClass could not be converted to string当我在网站上运行此部分代码时,发生错误,

1
2
3
4
5
6
7
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);
}

发生错误的行是这一行,

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

这行正在调用此函数,

1
2
3
4
5
6
7
8
9
10
11
   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();
        }
    }

我现在真的不是这个问题了,所以任何帮助都将非常有用。


userdata()函数很可能返回一个对象,而不是字符串。查看文档(或返回值var_dump)以找出需要使用的值。


我使用codeignator,但出现错误:

Object of class stdClass could not be converted to string.

对于这篇文章我得到我的结果

我在模型部分使用

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

从这篇文章中我使用

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

我解决了我的问题


一般摆脱

Object of class stdClass could not be converted to string.

尝试使用echo '

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

Will give the result as 2

wmh


尝试这个

1
return $query->result_array();

您在另一条评论中提到,您不希望get_userdata()函数返回stdClass对象吗?如果是这种情况,则应在该函数中注意这一行:

1
return $query->row();

在这里,CodeIgniter数据库对象" $ query"调用了row()方法,该方法返回一个stdClass。或者,您可以运行row_array(),以数组形式返回相同的数据。

无论如何,我强烈怀疑这不是问题的根本原因。能否给我们提供更多细节?尝试一些事情,让我们知道进展如何。我们无法处理您的代码,因此很难确切说明发生了什么。


wmh

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

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

然后就像对数组对象进行操作一样获取我的数据。

$data[0]['field_name']

无需担心类型转换或任何直接的问题。

所以对我有用。