xwishmasterx Posted April 12, 2011 Share Posted April 12, 2011 I have a table where a filed called "recruitment" can either be "0" or "1" Why does my below code not work? $sql_rec = ("SELECT * FROM teams WHERE team_id=".$_GET['t']." "); $rsrec = mysql_fetch_array($sql_rec); if($rsrec['recruitment'] == 1){ echo "Your team is <b>OPEN</b> for all members to join <br> <a href='changemode0.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";} elseif($rsrec['recruitment'] == 0){ echo "Your team is <b>CLOSED</b> for all members to join <br> <a href='changemode1.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>";} to ask in another way: how do I make the if statement to check if the value is "1" or "0"? Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/ Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 You aren't executing the query, and you have no logic in place to make sure the query succeeds before you attempt to access any result. This should produce the results you're looking for, and I added some very basic error handling/reporting. $query = ("SELECT recruitment FROM teams WHERE team_id={$_GET['t']}"); if( !$result = mysql_query($query) ) { echo "Query failed"; } else { if( mysql_num_rows($result) === 1 ) { $array = mysql_fetch_assoc($sql_rec); $one = "Your team is <b>OPEN</b> for all members to join <br><a href='changemode0.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>"; $zero = "Your team is <b>CLOSED</b> for all members to join <br><a href='changemode1.php?t=".$_GET['t']."&r=".$_GET['r']." '>(click here to change mod)</a>"; echo $array['recruitment'] == 1 ? $one : $zero; } else { echo 'Query returned empty result set'; } } Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200874 Share on other sites More sharing options...
xwishmasterx Posted April 12, 2011 Author Share Posted April 12, 2011 thanks but still not working. It always returns the "zero" value no matter what the value is... any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200879 Share on other sites More sharing options...
PFMaBiSmAd Posted April 12, 2011 Share Posted April 12, 2011 There's a $sql_rec left over from the original code. Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200880 Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 Add the line indicated in the comment, and post the output it generates. echo $array['recruitment'] == 1 ? $one : $zero; echo '<br>var_dump: ' . var_dump($array['recruitment']) . '<br>'; // <---- ADD THIS } else { Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200881 Share on other sites More sharing options...
xwishmasterx Posted April 12, 2011 Author Share Posted April 12, 2011 Deleting the "leftover" worked! Thanks a bunch. If any of you can tell me why I cannot use this url: (returns unexpected T-STRING) changemode1.php?t=".$_GET['t']."&r=".$_GET['r']." or perhaps tell me how I can rewrite it , then I'll be happy as a hamster! Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200884 Share on other sites More sharing options...
Pikachu2000 Posted April 12, 2011 Share Posted April 12, 2011 Whoops, I missed that one. But when you say you deleted it, I hope you mean you changed it to $result . . . As far as the error above, post the complete line that is generating it. Quote Link to comment https://forums.phpfreaks.com/topic/233545-simple-question-regarding-if-statements/#findComment-1200888 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.