dean7 Posted June 14, 2010 Share Posted June 14, 2010 Hi all, ive got a problem with this script, well part of it. Its the same script which i needed help with the other day which i manged to sort, but now its came to a bit im not to sure how to fix. $username = $_SESSION['username']; $location = $fetch_users_data->location; $carin = mysql_query("SELECT carin FROM users WHERE username = '$username'");// not sure about $car1 = mysql_query("SELECT * FROM garage WHERE owner = '$username' AND carin = '$carin'");; // not sure about $car = mysql_fetch_object($car1); ------------------------------------------------------------------- if ($car->speed <= "39"){ // Line 63 echo ("Your car must be 40mph or faster to win any races."); }elseif ($car->speed >= "40"){ // Code not needed at the mintue } Though that little part of the script, its giving me an error which says : Notice: Trying to get property of non-object in /home/www/*****/****.php on line 63 Anyone know why it giving me that error? Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/ Share on other sites More sharing options...
mattal999 Posted June 14, 2010 Share Posted June 14, 2010 $username = $_SESSION['username']; $location = $fetch_users_data->location; $carin = mysql_query("SELECT carin FROM users WHERE username = '$username'") or die(mysql_error()); // not sure about $car1 = mysql_query("SELECT * FROM garage WHERE owner = '$username' AND carin = '$carin'") or die(mysql_error()); // not sure about if(!$car = mysql_fetch_object($car1)) { die("Could not fetch row data - MySQL Error: ".mysql_error()); } ------------------------------------------------------------------- if ($car->speed <= "39"){ // Line 63 echo ("Your car must be 40mph or faster to win any races."); }elseif ($car->speed >= "40"){ // Code not needed at the mintue } Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071782 Share on other sites More sharing options...
dean7 Posted June 14, 2010 Author Share Posted June 14, 2010 $username = $_SESSION['username']; $location = $fetch_users_data->location; $carin = mysql_query("SELECT carin FROM users WHERE username = '$username'") or die(mysql_error()); // not sure about $car1 = mysql_query("SELECT * FROM garage WHERE owner = '$username' AND carin = '$carin'") or die(mysql_error()); // not sure about if(!$car = mysql_fetch_object($car1)) { die("Could not fetch row data - MySQL Error: ".mysql_error()); } ------------------------------------------------------------------- if ($car->speed <= "39"){ // Line 63 echo ("Your car must be 40mph or faster to win any races."); }elseif ($car->speed >= "40"){ // Code not needed at the mintue } I done that and it echoed: Could not fetch row data - MySQL Error: But dont know why it carnt fetch the data? Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071784 Share on other sites More sharing options...
mattal999 Posted June 14, 2010 Share Posted June 14, 2010 Ah! I think I've figured it out. Put this just above the mysql_fetch_object() part: echo mysql_num_rows($car1); Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071788 Share on other sites More sharing options...
dean7 Posted June 14, 2010 Author Share Posted June 14, 2010 Ah! I think I've figured it out. Put this just above the mysql_fetch_object() part: echo mysql_num_rows($car1); Did that and it echoed 0 Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071791 Share on other sites More sharing options...
l4nc3r Posted June 14, 2010 Share Posted June 14, 2010 Have you tried debugging it? Lol. Print out the SQL query and put it directly in to MySQL. See what happens. Print out a mysql_error() right after each mysql_query() function. Also, I see you commented a "not sure about" in your source code. If you're not sure about something, why not look it up in the manual? Oh, and Google is always your friend: http://www.google.com/search?q=debugging+mysql+php&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en___US369 Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071797 Share on other sites More sharing options...
dean7 Posted June 14, 2010 Author Share Posted June 14, 2010 Have you tried debugging it? Lol. Print out the SQL query and put it directly in to MySQL. See what happens. Print out a mysql_error() right after each mysql_query() function. Also, I see you commented a "not sure about" in your source code. If you're not sure about something, why not look it up in the manual? Oh, and Google is always your friend: http://www.google.com/search?q=debugging+mysql+php&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en___US369 Yeah ive tryed de-bugging it, been trying for ages lol. Rather new to php aswell so that dont help. Yeah were I commented "Not sure about" is part which im not sure if it would work. Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071801 Share on other sites More sharing options...
Adam Posted June 14, 2010 Share Posted June 14, 2010 As I think mattal999 was getting at, you're trying to use mysql_fetch_object() on either an empty resource, or a boolean value (mysql_query() returns false on error). You should validate that rows were actually returned from the SELECT statement with mysql_num_rows, before trying to fetch the data object. For example: if (mysql_num_rows($carl) > 0) { $car = mysql_fetch_object($carl); } If you still receive some PHP errors for using mysql_num_rows(), you have an error in your syntax. Quote Link to comment https://forums.phpfreaks.com/topic/204721-property-of-non-object/#findComment-1071806 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.