kaosjon Posted September 30, 2011 Share Posted September 30, 2011 Hi, i have written a section of code for my website and i have been trying to get it to work but whatever i try it will not work Here is the code $upgrade_user = mysql_query("SELECT * FROM user_info WHERE id='$id'"); while($row = mysql_fetch_array($upgrade_user)){ $real_balance = $row["$balance"]; $real_rank = $row["$rank"]; } echo $real_balance; if($real_rank == 'merchant' && $real_balance > '500'){ $n1x = '<a href="upgrades_info.php?userid=$id&rank=noble"><img src="images/noble.jpg" width="170" height="200" /></a>'; } else { $n1x = '<img src="images/noblex.jpg" width="170" height="200" />'; } It should outbut the first if, but instead it keeps displaying the else, i have checked the $real_rank and it matches merchant and the $real_balance is over 500. Any ideas? I am guessing its a simple error in the way i have written it, but i can't seem to get it to work. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2011 Share Posted October 1, 2011 What does the following show - var_dump($real_rank); var_dump($real_balance); Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274549 Share on other sites More sharing options...
kaosjon Posted October 1, 2011 Author Share Posted October 1, 2011 What does the following show - var_dump($real_rank); var_dump($real_balance); Hi, thanks for the help They show NULL NULL Thanks Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274550 Share on other sites More sharing options...
kaosjon Posted October 1, 2011 Author Share Posted October 1, 2011 Hi, i just tried changing the $id in the if ststement to an actual id (36) and it still displayed the variables as being NULL. Not sure what to do Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274552 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2011 Share Posted October 1, 2011 Do you actually have variables $balance and $rank that contain column names in your table so that $row["$balance"] and $row["$rank"], reference actual columns in your database table - $real_balance = $row["$balance"]; $real_rank = $row["$rank"]; Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274554 Share on other sites More sharing options...
kaosjon Posted October 1, 2011 Author Share Posted October 1, 2011 Hi, yes i do have columns in my database with those names, i managed to fix it, for now. Heres my final code not sure what i done, just played around with it. Thanks for your help $upgrade_rank = mysql_query("SELECT rank FROM user_info WHERE id='$id'"); $user_array = mysql_fetch_assoc($upgrade_rank); $real_rank = $user_array['rank']; $upgrade_balance = mysql_query("SELECT balance FROM user_info WHERE id='$id'"); $user_array = mysql_fetch_assoc($upgrade_balance); $real_balance = $user_array['balance']; var_dump($real_rank); var_dump($real_balance); if(($real_rank == 'merchant') && ($real_balance > 500)){ $n1x = '<a href="upgrades_info.php?userid=$id&rank=noble"><img src="images/noble.jpg" width="170" height="200" /></a>'; } else { $n1x = '<img src="images/noblex.jpg" width="170" height="200" />'; } Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274558 Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2011 Share Posted October 1, 2011 $user_array['rank'] (an associative index named 'rank') is not the same as $row["$rank"] (a php variable named $rank inside of a double-quoted string.) You now have two queries instead of one. I hope you are not trying to make a real application that must perform well with a lot of concurrent visitors. <?php $result = mysql_query("SELECT rank,balance FROM user_info WHERE id='$id'"); $user_array = mysql_fetch_assoc($result); $real_rank = $user_array['rank']; $real_balance = $user_array['balance']; Edit: Or even simpler - <?php $result = mysql_query("SELECT rank,balance FROM user_info WHERE id='$id'"); list($real_rank,$real_balance) = mysql_fetch_row($result); Quote Link to comment https://forums.phpfreaks.com/topic/248207-if-statement-not-displaying-right-part/#findComment-1274720 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.