Home >php教程 >php手册 >Cannot use object of type stdClass as array

Cannot use object of type stdClass as array

WBOY
WBOYOriginal
2016-06-13 09:53:501278browse

在我写php是会出现,如下图代码提供错误 Fatal error: Cannot use object of type stdClass as array

在我写php是会出现,如下图代码提供错误

Fatal error: Cannot use object of type stdClass as array

这个问题看提示如果没有经验的朋友可能看不出所以然来,下面我们就来看看我这段代码。

 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[]数据访问形式,当然就会提示出错哦。

好了问题解决了吧。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn