TecTao Posted March 16, 2012 Share Posted March 16, 2012 I'm getting frusted over an if else statememt that won't work. It is calling one variable from the table. If the variable is in the table it will display one thing, if the variable is absent, it should display something else. I've tried !empty, isset, even $num_rows =1, but the content in the else doesn't display. Here's the code: <? $result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { extract($row); if (!empty( $MyCode )) { echo "here is the code word"; } else { echo "you need to go back and get the code"; } $row_count++; } mysql_close; ?> When the code variable is correct the "here is the code word" displays. when the code variable is incorrect, what should appear as "you need to go back and get the code" is just blank. Any thoughts on how to structure the query in such a way that eh incorrect text appears? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/ Share on other sites More sharing options...
litebearer Posted March 16, 2012 Share Posted March 16, 2012 think about what your query is asking, then think about what happens with your WHILE if no record was found Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328110 Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 So what is the value of $MyCode? extract($row); var_dump($MyCode); Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328111 Share on other sites More sharing options...
Muddy_Funster Posted March 16, 2012 Share Posted March 16, 2012 erm....I think this could have something to do with it if (!empty( $MyCode )) could just be me, but looking at the code it's never realy gonna be empty...is it? P.S. **slaps wrist** stop using short open tags Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328119 Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 erm....I think this could have something to do with it if (!empty( $MyCode )) could just be me, but looking at the code it's never realy gonna be empty...is it? Why? Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328123 Share on other sites More sharing options...
TecTao Posted March 16, 2012 Author Share Posted March 16, 2012 Well it's not really empty, if it doesn't return anything is just doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328132 Share on other sites More sharing options...
litebearer Posted March 16, 2012 Share Posted March 16, 2012 consider the possibilities of this $result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " ) or die("SELECT Error: ".mysql_error()); 1. error = code stops 2. no results = never enters while loop 3. 1 or more results = enter while loop = 2 possibilities depending upon value of original $MyCode in the query a) either ALL sets contain a value for MyCode; or b) ALL sets contain no value for MyCode Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328279 Share on other sites More sharing options...
Pikachu2000 Posted March 16, 2012 Share Posted March 16, 2012 Does your php.ini file contain the following directives? error_reporting = -1 display_errors = On Quote Link to comment https://forums.phpfreaks.com/topic/259065-if-else-delima-returns-nothing-in-the-else/#findComment-1328293 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.