Jump to content

Trying to get property of non-object (Newbie)


etherboo

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...

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.

Link to comment
Share on other sites

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.