I'm getting the error:
我得到错误:
"Fatal error: Cannot use object of type stdClass as array in" on line 183
“致命错误:在第183行中不能使用stdClass类型的对象作为数组
From this code:
从这段代码:
$getvidids = $ci->db->query(
"SELECT * FROM videogroupids " .
"WHERE videogroupid='$videogroup' AND used='0' LIMIT 10");
foreach ($getvidids->result() as $row){
$vidid = $row['videoid']; //This is line 183
}
Anyone know what's wrong with the above code? Or what this error means?
有人知道上面的代码有什么问题吗?或者这个误差意味着什么?
3 个解决方案
#1
60
CodeIgniter returns result rows as objects, not arrays. From the user guide:
CodeIgniter将结果行作为对象返回,而不是数组。从用户指南:
result()
This function returns the query result as an array of objects, or an empty array on failure.
此函数以对象数组或失败的空数组的形式返回查询结果。
You'll have to access the fields using the following notation:
您必须使用以下符号访问字段:
foreach ($getvidids->result() as $row) {
$vidid = $row->videoid;
}
#2
15
if you really want an array instead you can use:
如果你真的想要一个数组,你可以使用:
$getvidids->result_array()
which would return the same information as an associative array.
它将返回与关联数组相同的信息。
#3
-3
Sorry.Though it is a bit late but hope it would help others as well . Always use the stdClass object.e.g
对不起。虽然有点晚了,但希望它也能帮助别人。总是使用stdClass对象
$getvidids = $ci->db->query("SELECT * FROM videogroupids WHERE videogroupid='$videogroup' AND used='0' LIMIT 10");
foreach($getvidids->result() as $key=>$myids)
{
$vidid[$key] = $myids->videoid; // better methodology to retrieve and store multiple records in arrays in loop
}
#1
60
CodeIgniter returns result rows as objects, not arrays. From the user guide:
CodeIgniter将结果行作为对象返回,而不是数组。从用户指南:
result()
This function returns the query result as an array of objects, or an empty array on failure.
此函数以对象数组或失败的空数组的形式返回查询结果。
You'll have to access the fields using the following notation:
您必须使用以下符号访问字段:
foreach ($getvidids->result() as $row) {
$vidid = $row->videoid;
}
#2
15
if you really want an array instead you can use:
如果你真的想要一个数组,你可以使用:
$getvidids->result_array()
which would return the same information as an associative array.
它将返回与关联数组相同的信息。
#3
-3
Sorry.Though it is a bit late but hope it would help others as well . Always use the stdClass object.e.g
对不起。虽然有点晚了,但希望它也能帮助别人。总是使用stdClass对象
$getvidids = $ci->db->query("SELECT * FROM videogroupids WHERE videogroupid='$videogroup' AND used='0' LIMIT 10");
foreach($getvidids->result() as $key=>$myids)
{
$vidid[$key] = $myids->videoid; // better methodology to retrieve and store multiple records in arrays in loop
}