dean7 Posted May 1, 2010 Share Posted May 1, 2010 Hi all, ive got got this piece of code: if ($fetch->money < $price){ echo "You dont have enough money to upgrade your car!"; }elseif ($fetch->money >= $price){ $new_money=$fetch->money-$price; $new_up="$next_1-$upgrades[1]-$upgrades[2]-$upgrades[3]-$upgrades[4]-$upgrades[5]-$upgrades[6]-$upgrades[7]"; mysql_query("UPDATE garage SET upgrades='$new_up' WHERE id='$car' AND owner='$username'"); mysql_query("UPDATE users SET money='$new_money' WHERE username='$username'"); $speed = mysql_query("SELECT speed FROM garage WHERE id='$car' AND owner='$username'"); mysql_query("UPDATE garage SET speed='$speed+5' WHERE id='$car' AND owner='$username'") OR DIE ("Couldnt add 5mph"); echo "Car upgraded!"; } } But there seems to be a problem with it. When i echo the speed out from database it says: Resource id #15+5 MPH. Allthough what I though it should do is update that table but add 5 to the current ammout. Anyone know why its inserting : Resource id #15+5 MPH into the database instaid of 5? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/ Share on other sites More sharing options...
foobarbaz Posted May 1, 2010 Share Posted May 1, 2010 $speed = mysql_query("SELECT speed FROM garage WHERE id='$car' AND owner='$username'"); mysql_query returns a resource ID on success. You must use a function such as mysql_fetch_array or mysql_fetch_assoc to return an array with the result. Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051626 Share on other sites More sharing options...
Mchl Posted May 1, 2010 Share Posted May 1, 2010 mysql_query returns a resource ID on success. Actually it returns a resource, which returns it's ID when cast to string. Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051627 Share on other sites More sharing options...
dean7 Posted May 1, 2010 Author Share Posted May 1, 2010 But how would I code a function just to show the speed of the car? Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051628 Share on other sites More sharing options...
foobarbaz Posted May 1, 2010 Share Posted May 1, 2010 This should work: $result = mysql_fetch_assoc(mysql_query("SELECT speed FROM garage WHERE id='$car' AND owner='$username'")) OR DIE(mysql_error()); $speed = $result['speed']; mysql_query("UPDATE garage SET speed='$speed+5' WHERE id='$car' AND owner='$username'") OR DIE ("Couldnt add 5mph"); echo "Car upgraded!"; Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051630 Share on other sites More sharing options...
dean7 Posted May 1, 2010 Author Share Posted May 1, 2010 This should work: $result = mysql_fetch_assoc(mysql_query("SELECT speed FROM garage WHERE id='$car' AND owner='$username'")) OR DIE(mysql_error()); $speed = $result['speed']; mysql_query("UPDATE garage SET speed='$speed+5' WHERE id='$car' AND owner='$username'") OR DIE ("Couldnt add 5mph"); echo "Car upgraded!"; I changed that but it made no different :S. But ive also found out that everytime i add a new part to the car it adds another +5 at the end. So example: Resource id #15+5+5 MPH Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051635 Share on other sites More sharing options...
foobarbaz Posted May 1, 2010 Share Posted May 1, 2010 It was being added as a string, you'll need to reset it and treat it separately: mysql_query("UPDATE garage SET speed='" . $speed+5 . "' WHERE id='$car' AND owner='$username'") OR DIE ("Couldnt add 5mph"); Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051636 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 why not simply add 5 to it without having to select it mysql_query("UPDATE garage SET speed=speed + 5 WHERE id='$car' AND owner='$username'") OR DIE ("Couldnt add 5mph"); that'll add 5 to the current value without having to do a whole other query Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051639 Share on other sites More sharing options...
dean7 Posted May 1, 2010 Author Share Posted May 1, 2010 Thanks mikesta707 that worked! Also thanks to the others for the help they gave. Quote Link to comment https://forums.phpfreaks.com/topic/200395-resource-id/#findComment-1051641 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.