Callum
Callum

Reputation: 63

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 ..."

Can someone tell me where I am going wrong?

Upvotes: 0

Views: 1013

Answers (1)

Shi
Shi

Reputation: 4258

Have a look at the return value of mysqli::query(). It can return FALSE in case of an error. So $result will be FALSE and FALSE->fetch_object() doesn't make sense - and it does call a member function on a non-object.

For debugging, you can output error messages:

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

Upvotes: 1

Related Questions