Jump to content

Recommended Posts

Hey,

 

I created a method to allow me to pull the first 5 results in my database and display them.

 

  public function storeFront()
  {
    $mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);
    $result = $mysqli->query("SELECT id, name, description, price, quantities 
                              FROM products 
                              LIMIT 5");
    
    echo '<div id="newest">';
    while ($row = $result->fetch_object())
    {
      echo '<div class="block">'
              .'<b>Name: </b>' .$row->name. '<br />'
              .'<b>Description: </b>' .$row->description. '<br />'
              .'<b>Price: </b>'.$row->price.'<br />'
              .'<b>Quantities: </b>' .$row->quantities. '<br />'
            .'</div>';
    }
    echo '</div>';
  }

 

I'm getting an error:

 

Fatal error: Call to a member function fetch_object() on a non-object in C:\Users\9three\Desktop\Server\htdocs\cart\package\cart.php on line 28

 

line 28 is: while ($row = $result->fetch_object())

 

fetch_object() is the correct method and I seem to be passing it correct. Does anyone see where I went wrong?

Link to comment
https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/
Share on other sites

Oosp, I'm sorry, I missed the 'i' (as in mysqli), I usually use the mysql functions. 

 

Your original code looks OK, unless there is something wrong with the query.  Take taquitosensei's advice, and print_r($result) to see if it is valid.  Also, make sure error_reporting is at E_ALL incase there is a warning or notice that you are not seeing.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.