php 类 stdClass 的对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3607550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object of class stdClass could not be converted to string
提问by sea_1987
I am having a problem with PHP at the moment, I am getting this error,
我现在遇到了 PHP 问题,出现了这个错误,
Object of class stdClass could not be converted to string
the error occurs when I run this portion of code in my site,
Object of class stdClass could not be converted to string
当我在我的站点中运行这部分代码时发生错误,
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.
我现在真的不知道这个问题是什么,所以任何帮助都会很棒。
回答by Pekka
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.
最有可能的是,该userdata()
函数返回的是一个对象,而不是一个字符串。查看文档(或 var_dump 返回值)以找出您需要使用的值。
回答by roshan
I use codeignator and I got the error:
我使用 codeignator 并收到错误消息:
Object of class stdClass could not be converted to string.
类 stdClass 的对象无法转换为字符串。
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
我解决了我的问题
回答by N?s????? ?
In General to get rid of
一般要摆脱
Object of class stdClass could not be converted to string.
类 stdClass 的对象无法转换为字符串。
try to use echo '<pre>'; print_r($sql_query);
for my SQL Query got the result as
尝试echo '<pre>'; print_r($sql_query);
用于我的 SQL 查询得到的结果为
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
为了访问有不同的方法,例如:num_rows、row、rows
echo $query2->row['option_id'];
Will give the result as 2
将结果为2
回答by Roopchand
try this
尝试这个
return $query->result_array();
回答by treeface
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:
您在另一条评论中提到您不希望 get_userdata() 函数返回 stdClass 对象?如果是这种情况,您应该注意该函数中的这一行:
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.
在这里,CodeIgniter 数据库对象“$query”调用了它的 row() 方法,该方法返回一个 stdClass。或者,您可以运行 row_array() 以数组形式返回相同的数据。
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.
无论如何,我强烈怀疑这不是问题的根本原因。你能给我们提供更多细节吗?玩一些东西,让我们知道它是怎么回事。我们无法使用您的代码,因此很难确切说明发生了什么。
回答by Vicky Salunkhe
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 = $this->db->get('table_name')->result_array();
and then fetched my data just as you operate on array objects.
然后就像您对数组对象进行操作一样获取我的数据。
$data[0]['field_name']
$data[0]['field_name']
No need to worry about type casting or anything just straight to the point.
无需担心类型转换或任何直截了当的事情。
So it worked for me.
所以它对我有用。