Cannot use object of type stdClass as array
在我写php是会出现,如下图代码提供错误
Fatal error: Cannot use object of type stdClass as array
这个问题看提示如果没有经验的朋友可能看不出所以然来,下面我们就来看看我这段代码。
<?php
function get_userinfo($id) {
$sql = "Select * from @#_debbs where username='$id'";
$result = mysql_query($sql) or die('e');
if (mysql_num_rows($result)) {
$rs = mysql_fetch_object($result);
return array(
$rs['sex'],
$rs['born'],
$rs['reg_time'],
'/boke/' . $rs['my_photo'],
$rs['id']
);
} else {
return array(
'参数错误',
'未知',
'未知',
'未知',
'未知',
'未知'
);
}
}
?>各位看出来是怎么回事了吗?
$rs = mysql_fetch_object($result);
看看上面这或,再看看下面
return array($rs['sex'],$rs['born'],$rs['reg_time'],'/boke/'.$rs['my_photo'],$rs['id']);
看出来了没?我上面是以objcect后下面用的时间又用$rs[]数据访问形式,当然就会提示出错哦。
好了问题解决了吧。
教程地址:http://www.phprm.com/develop/fs532.html
欢迎转载!但请带上文章地址^^