etherboo Posted March 23, 2010 Share Posted March 23, 2010 Hello, I'm currently in the process of trying to learn php. I've been working on an issue with something I am trying to code from a book I purchased "PHP and MySQL Web Development 5th Ed." by Luke Welling and Laura Thomson. So far the book has been great, but I'm trying to figure out using MySQL and php. When I try to run this particular script for a query for a book store, I get the error: Notice: Trying to get property of non-object in C:\php-book\ch11\results.php on line 37. This is the code: $query = "select * from books where ".$searchtype."like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for($i=0; $i<$num_results; $i++) { $row = $result->fetch_assoc(); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br />Author: "; echo stripslashes($row['author']); echo "<br /> ISBN: "; echo stripslashes($row['isbn']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; } $result->free(); $db->close(); Line 37 is specifically: $num_results = $result->num_rows; I've been bashing my head for a couple of hours here, thank you in advance to whoever can offer any help. Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/ Share on other sites More sharing options...
nafetski Posted March 23, 2010 Share Posted March 23, 2010 Well, learning PHP and learning OOP (object oriented programming) at the same time is a lot to chew...so don't feel alone One of the beautiful parts about PHP is how descriptive the errors can be. "Notice: Trying to get property of non-object..." Basically it's saying that you're trying to treat a non-object variable as if it WERE an object. I'm not too sure what your database object ($db) looks like, but I'm willing to bet that when you call the query method (So, to access a method or property in an object you use "->") it doesn't RETURN an object (instead it probably returns an array). A quick fix for your code, is instead of using that num_rows property...leave it out, and then do $num_rows = count($result); Let me know if that helps! Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1030281 Share on other sites More sharing options...
AdRock Posted March 24, 2010 Share Posted March 24, 2010 I think you may have forgotten the () after numrows numrows() I bet it is a method in your database class Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1030871 Share on other sites More sharing options...
etherboo Posted March 30, 2010 Author Share Posted March 30, 2010 I'm sorry for my long reply. My classwork really picked up, and I've had to lay off this for a bit. I plan on picking it up while I have more free time. Your solution resolved my error, but I'm still having another problem. Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1034267 Share on other sites More sharing options...
etherboo Posted March 30, 2010 Author Share Posted March 30, 2010 I think you may have forgotten the () after numrows numrows() I bet it is a method in your database class I tried that at first, it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1034268 Share on other sites More sharing options...
bakhtn Posted November 24, 2011 Share Posted November 24, 2011 theres an error in your SQL statement, run the sql seperately on the mysql monitor, and fix the error , that should sort it out. Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1290984 Share on other sites More sharing options...
xyph Posted November 24, 2011 Share Posted November 24, 2011 bakhtn has it right, and I'll go in more depth. $result is populated from $db->query($query);. If mysqli->query doesn't return an object, it returns FALSE. You should check the manual for find out why it would do this, and how to fix it. Coding in an if/else that checks if $result is FALSE is probably a good idea as well. Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1290989 Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2011 Share Posted November 24, 2011 old thread... Quote Link to comment https://forums.phpfreaks.com/topic/196187-trying-to-get-property-of-non-object-newbie/#findComment-1290994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.