简体   繁体   English

从数据库获取数据的问题

[英]Problems with fetching data from a database

My Current Code: 我当前的代码:

<?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.";
                }
?>

Returns the error: "Fatal error: Call to a member function fetch_object() on a non-object in ..." 返回错误:“致命错误:在...中的非对象上调用成员函数fetch_object()”

Can someone tell me where I am going wrong? 有人可以告诉我我要去哪里了吗?

Have a look at the return value of mysqli::query() . 看一下mysqli :: query()的返回值。 It can return FALSE in case of an error. 如果发生错误,它可以返回FALSE So $result will be FALSE and FALSE->fetch_object() doesn't make sense - and it does call a member function on a non-object. 因此$result将为FALSEFALSE->fetch_object()则没有意义-并且它确实在非对象上调用了成员函数。

For debugging, you can output error messages: 为了进行调试,您可以输出错误消息:

$result = $mysqli->query('SELECT title, description, portfolio FROM Portfolio;');
if (!$result) {
  die($mysqli->error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM