9three Posted June 19, 2009 Share Posted June 19, 2009 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 More sharing options...
DavidAM Posted June 19, 2009 Share Posted June 19, 2009 Looking at your code, $result is not an object, it is a resource; and fetch_object() is not an object method, it is a function. I think the correct call would be: $row = fetch_object($result) Link to comment https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/#findComment-859604 Share on other sites More sharing options...
9three Posted June 19, 2009 Author Share Posted June 19, 2009 Hmm no that's not it. Link to comment https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/#findComment-859612 Share on other sites More sharing options...
taquitosensei Posted June 19, 2009 Share Posted June 19, 2009 you could do a print_r on $result make sure it's an object. I'm not sure what mysqli->query would return if there was an error in your sql. Link to comment https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/#findComment-859627 Share on other sites More sharing options...
DavidAM Posted June 19, 2009 Share Posted June 19, 2009 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. Link to comment https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/#findComment-859673 Share on other sites More sharing options...
9three Posted June 19, 2009 Author Share Posted June 19, 2009 I figured it out. I had a typo in my constructor $this->database = $databse is suppose to be $this->database = $database. I forgot the 'a' :-\ thank you anway Link to comment https://forums.phpfreaks.com/topic/162915-solved-call-to-a-non-object-error/#findComment-859676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.