Jump to content

[SOLVED] Call to a non-object error


9three

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.