我当前的代码:
<?php
if(require("connect.php"))
{
$result = $mysqli->query('SELECT title, description, portfolio FROM Portfolio;');
while($o = $result->fetch_object())
{
$title=htmlspecialchars($o->title);
$description=htmlspecialchars($o->description);
$portfolio=htmlspecialchars($o->portfolio);
echo <<<ARTBOX
<div class="artbox">
<div class="photocontainer">
<img src="$portfolio" alt="$title" />
</div>
<div class="artboxtitle">
$title
</div>
<div class="artboxdescription">
$description
</div>
<div class="artboxenquiry">
<img src="images/enquiry.png" alt="Make an Enquiry" href="#" />
</div>
</div>
ARTBOX;
}
}
else
{
echo "An error occurred, please try again.";
}
?>
返回错误:“致命错误:在...中的非对象上调用成员函数fetch_object()”
有人可以告诉我哪里出错了吗?
答案 0 :(得分:1)
查看mysqli::query()的返回值。如果出现错误,它可以返回FALSE
。因此$result
将是FALSE
而FALSE->fetch_object()
没有意义 - 它会在非对象上调用成员函数。
对于调试,您可以输出错误消息:
$result = $mysqli->query('SELECT title, description, portfolio FROM Portfolio;');
if (!$result) {
die($mysqli->error);
}