Jump to content

Property of non object error


afish674

Recommended Posts

I'm having some problem with my code. It says that I'm trying to get the property of a non-object on line 24. However I'm using the method for get object. So I don't really understand why it says its not an object. As you can see I have tried "fetch(PDO::FETCH_OBJ)" and "fetchObject()" to no avail. I assume these two do the same thing? I am very new to PHP.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <title>Project 7-4: Building A Login Form</title>  </head>  <body>    <h2>Project 7-4: Building A Login Form</h2><?php    // if form not yet submitted    // display form    if (!isset($_POST['submit'])) {	    try{	    $pdo = new PDO('mysql:dbname=music;host=localhost','root','Rockon!');	   } catch (PDOException $e){	    die("ERROR: Could not connect: " . $e->getMessage());	   }  //Get artists  $artists = array();	   $sql = "SELECT artist_id, artist_name FROM artists";	   if ($result = $pdo->query($sql)) {	    while($row = $result->fetchObject());	   $artists[$row->artist_id] = $row->artist_name;	   }  $ratings = array();  //Get ratings  $sql = "SELECT rating_id, rating name FROM ratings";  if ($result = $pdo->query($sql)){   while($row = $result->fetch(PDO::FETCH_OBJ));	   $ratings[$row->rating_id] = $row->rating_name;  } }?>    <form method="post" action="add_songs.php">	 <label for="songName">Song Name:</label>	  <input type="text" name="songName"/>	  <label for="songArtist">Song Artist:</label>	  <select name="songArtist">	   <!--unset($pdo);-->	  </select>	  <input type="submit" name="submit" value="Add Record">	    </form>   </body></html>

Thanks for your help.

Link to comment
Share on other sites

Look closely at those two lines:

            while($row = $result->fetchObject());           $artists[$row->artist_id] = $row->artist_name;

If you look close enough, you'll notice the second line is NOT part of the loop.

while($row = $result->fetchObject())

declares a loop, and the very next symbol,

;

ends the one statement that would then become the loop's body.But the loop ends when $rows becomes false, due to fetchObject() returning false when there are no more rows. So by the time the next line executes, $row is not an object => you get this error.The same applies to

   while($row = $result->fetch(PDO::FETCH_OBJ));           $ratings[$row->rating_id] = $row->rating_name;

as well.

Link to comment
Share on other sites

Ah! Thanks for that. I need to get my eye in. I just get into the habit of ending every statement with a ";" without really thinking about it. Cheers for your help!
There's no need to suppress that habit.Just add the additional habit of adding braces to every control structure, e.g.
            while($row = $result->fetchObject()) {                $artists[$row->artist_id] = $row->artist_name;            }

Link to comment
Share on other sites

Most of the code is from a book I've been using a book to learn, they didn't use braces in this case. I did wonder why though as normally you do. I will do so in future.

			while($row = $result->fetchObject()) {				$artists[$row->artist_id] = $row->artist_name;			}

I did wonder what the above code snippet actually does though. The assignment operator normally makes the bit on the left match the bit on the right, but I'm not sure it is doing that in this case? Edited by afish674
Link to comment
Share on other sites

Also, I don't understand why in a bit of code which I haven't included in the first block occurs:

<select name="songArtist">	   <?php	   foreach ($artists as $k => $v){	   echo "<option value=\"$k\">".$v."</option>";	   }	   ?>	  </select>

Why is the foreach loop "$artists as $k => $v"?When $artists as $a would seemingly work too.

Link to comment
Share on other sites

Most of the code is from a book I've been using a book to learn, they didn't use braces in this case. I did wonder why though as normally you do. I will do so in future.
Because if the body is just one statement, adding the braces is optional, so the author took advantage of that to make the code feel cool. I'm all for code that feels cool, but IMHO, a beginners' book should explicitly clarify any "cool" stuff used, and in fact, it should absolutely avoid using cool stuff, with the exception of a special chapter dedicated to that.Other "cool" constructs you might occasionally find are (just to give you some terms to chew on :lol:) object invokes, self returning mutable objects (think jQuery), hex/octal number notations and include returns.
I did wonder what the above code snippet actually does though. The assignment operator normally makes the bit on the left match the bit on the right, but I'm not sure it is doing that in this case?
Yes, the assignment operator assigns the value on the right to the variable on the left. But the returned value of the assignment operator is the assigned value.So, whether you have
while($row = $result->fetchObject())

or just

while($result->fetchObject())

The loop will still be executed the same number of times.The difference is that with the second variant, you have no way of reading the data returned from fetchObject(), since the returned object is discarded after the check. With the first variant, the returned value from the assignment operator is discarded, but a copy of fetchObject()'s returned value is still in the $row variable.

Why is the foreach loop "$artists as $k => $v"?When $artists as $a would seemingly work too.
the names $k and $v are arbitrary. They can be anything you want, as long as you keep using those same names within the foreach.This form was used so that within the foreach, you can read the exact array key ($k) associated with the value ($v), which is needed in this case.
Link to comment
Share on other sites

Thanks for taking the time to explain all that to me. Really appreciate it! I agree with your point about beginners books too. The author of this one tends to change minor things, but when you're trying to understand the code line by line, you suddenly find yourself going "Whoa! Where did that come from?!" Its quite frustrating.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...