请问如下报错 Object of class stdClass could not be converted 如何处理

qq_19444517 2017-05-17 10:13:39


代码报错:按F12 发现如下报错:

<font size='1'><table class='xdebug-error xe-catchable-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Catchable fatal error: Object of class stdClass could not be converted to string in D:\wamp64\www\practice\sy10_1.php on line <i>71</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>239776</td><td bgcolor='#eeeeec'>{main}( )</td><td title='D:\wamp64\www\practice\sy10_1.php' bgcolor='#eeeeec'>...\sy10_1.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0156</td><td bgcolor='#eeeeec' align='right'>252296</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.printf' target='_new'>printf</a>
( )</td><td title='D:\wamp64\www\practice\sy10_1.php' bgcolor='#eeeeec'>...\sy10_1.php<b>:</b>71</td></tr>


代码如下:
<html>
<head>
<title>部门员工查询</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 15px; font-family: "幼圆";}
-->
</style>
<script>
//初始化函数
function GetXmlHttpObject()
{
var XMLHttp=null;
try
{
XMLHttp=new XMLHttpRequest();
}
catch (e)
{
try
{
XMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
XMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return XMLHttp;
}
//选择列表框中选项时触发run()函数
function run()
{
XMLHttp=GetXmlHttpObject();
var Depart=document.getElementById("Depart").value;
var url="SY10_1_pro.php"; //服务器端在SY10_1_pro.php中处理
url=url+"?Depart="+Depart; //url地址,以GET方式传递
url=url+"&sid="+Math.random(); //添加一个随机数,以防服务器使用缓存的文件
XMLHttp.open("GET",url,true); //通过GET方式打开XMLHTTP对象
XMLHttp.send(null); //向服务器发送HTTP请求,请求内容为空
XMLHttp.onreadystatechange = function() //定义响应处理函数
{
if (XMLHttp.readyState==4&&XMLHttp.status==200)
{
//在"txthint"上显示返回信息
document.getElementById("txthint").innerHTML=XMLHttp.responseText;
}
}
}
</script>
</head>
<body bgcolor="D9DFAA">
<div align=center><font face="幼圆" size="5" color="#008000"><b>部门员工查询</b></font></div>
<form action="" method="get">
<div align="center">
<select name="Depart" id="Depart" onchange="run()">
<option>请选择</option>
<?php
$conn= new mysqli('localhost','root','','YGGL');
if (mysqli_connect_errno($conn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($conn, "utf8"); //将字符集设为uft8
$sql="select * from Departments";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row= $result->fetch_object())
{
printf($row ->DepartmentName);
}
// Free result set
mysqli_free_result($result);
}

mysqli_close($conn);
?>
</select>
</div>
</form>
<!-- 设置id为"txthint"的div标记,用于显示返回信息 -->
<div id="txthint"></div>
</body>
</html>
...全文
3584 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_19444517 2017-05-17
  • 打赏
  • 举报
回复
非常感谢你的帮助: 我用 print_r($row); 发现数组是正常显示的。 后面重新启用printf($row ->DepartmentName); 又可以正常显示。 不明白原因,但是已经正常了
xuzuning 2017-05-17
  • 打赏
  • 举报
回复

71 行是这个吧? printf($row ->DepartmentName);
但是没道理的
你 print_r($row); 看看输出的是什么

20,397

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧