Hi Guys.

Ive got a small problem where I'm trying to display rows from my DB

Here is the code that I'm currently using:

<?php
$results = $sqli  = ("SELECT * FROM `books` ORDER BY `ISBN` ASC");

if ($results) { 

    //fetch results set as object and output HTML
    while($obj = $results->fetch_object())
    {
        echo '<div class="book">'; 
        echo '<form method="post" action="cart_update.php">';
        echo '<div class="book-thumb"><img src="images/'.$obj->BookImage.'"></div>';
        echo '<div class="book-content"><h3>'.$obj->Title.'</h3>';
        echo '<div class="book-desc">'.$obj->BookDesc.'</div>';
        echo '<div class="book-info">';
        echo 'Price '.$currency.$obj->Price.' | ';
        echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
        echo '<button class="add_to_cart">Add To Cart</button>';
        echo '</div></div>';
        echo '<input type="hidden" name="ISBN" value="'.$obj->ISBN.'" />';
        echo '<input type="hidden" name="type" value="add" />';
        echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
        echo '</form>';
        echo '</div>';
    }

}
?>

I keep getting the following error :
( ! ) Fatal error: Call to a member function fetch_object() on a non-object in C:\wamp\www\books.php on line 26

Line 26 is: while($obj = $results->fetch_object())

Any idea on what ive done wrong?

Thanks!

Member Avatar for diafol

What's this supposed to be doing?

$results = $sqli  = ("SELECT * FROM `books` ORDER BY `ISBN` ASC");

Looks like it's just storing a string.

You're not actually making a call to the database. If I remember correctly, for mysqli it's something like $connectionVar->query("query string") or something similar. Currently, you're just storing a string in the mysqli var and the results var but no action is occuring.

EDIT for clarity:
If my mysqli knowledge is ripe enough, you first need to make sure you have connected to the db with something like $connection = mysqli_connect(connection arguments) where the arguments are things like your username, password, db name, host path, etc. Then do a $results = $connection->query("SELECT * FROM blah blah"), then iterate over $results to get your information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.