dtd58256 2012-03-18 12:10
浏览 128
已采纳

类stdClass的对象无法转换为字符串

I dont know why but I get this error :

Catchable fatal error: Object of class stdClass could not be converted to string

For this code:

$sql = "SELECT * FROM player ORDER BY score DESC LIMIT $begin";
                    $arr = array();
                    while($obj = mysql_fetch_object(mysql_query($sql))) {
                            //$arr[] = $obj;
                            echo $obj;
                    }
  • 写回答

2条回答 默认 最新

  • dongzhangji4824 2012-03-18 12:25
    关注

    You're using mysql_fetch_object (which returns an object) and then trying to output it as a string. That won't work.

    In your case, you should use a function that is capable of printing the contents of the object. There are many but the most straight-forward ones are print_r or var_dump. if you're outputting in an HTML context, you might wan to wrap a <pre> tag around the output to make it more readable or click "View source" in your browser.

    If you're writing your own objects, they can also be "converted" to strings by implementing the __toString() magic method

    Also, as people have said in the comments, your code will run the query on each pass through the loop. Check out the documentation here and read the examples.

    Note: You might have simplified the example for SO's sake, but make sure to sanitize your variables (e.g. $begin) to avoid SQL injections!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。