ultraloveninja Posted April 27, 2011 Share Posted April 27, 2011 Hi there! So, I remember doing this correctly some time ago, but now I've forgotten how. I am try in to get it to echo out something if the results from mysql are empty. I can get it to return the results if there is something in the database, but it's not echoing out the message from the case statement. I can't remember how to get it to look for "nothing". Here's my php code: <?php $sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc"; $result = mysql_query($sql,$conn); while ($row = mysql_fetch_array($result)){ $petname = $row['petname']; $desc = $row['description']; $family = $row['familyname']; if ($row=="0") { echo "No pet tributes for the letter."; } else { echo <<<END <p><strong>$petname</strong><br /> $desc<br /> <em>- $family</em></p> END; } } ?> I am not sure if I have the if, else in the right place either. Any help is greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/ Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 i would add this while ($row = mysql_fetch_array($result)){ $petname = $row['petname']; $desc = $row['description']; $family = $row['familyname']; $num_rows = mysql_num_rows($result); if ($num_rows=="0") { echo "No pet tributes for the letter."; } else { echo <<<END Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207046 Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 you could also have your if,else statement outside of your while loop...which is what i would do Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207049 Share on other sites More sharing options...
ultraloveninja Posted April 27, 2011 Author Share Posted April 27, 2011 Ahhh...ok. Yeah, I did have it here originally: $sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc"; $result = mysql_query($sql,$conn); if ($result=="0") { echo "<p>No pet tributes for the letter.</p>"; } else { ... but it wasn't working, I thought that I would need it to check against the rows. But now that I am looking at it your suggestion is the missing link. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207051 Share on other sites More sharing options...
PaulRyan Posted April 27, 2011 Share Posted April 27, 2011 Why don't you try something like this? <?PHP $letter = mysql_real_escape_string($_GET['letter']); $myQuery = "SELECT * FROM tbl_tribue WHERE letter = '$letter' ORDER BY letter ASC"; if($doQuery = mysql_query($myQuery)) { if(mysql_num_rows($doQuery)) { while($row = mysql_fetch_assoc($doQuery)) { echo '<p><strong>', $row['petname'] ,'</strong><br />', $desc ,'<br />', '<em>- ', $family ,' </em></p>'; } } else { echo 'No pet tributes for the letter "<strong>', $letter ,'</strong>"'; } } else { echo 'This query failed: "', $myQuery ,'"'; } ?> Tell me if this works, as it's untested. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207052 Share on other sites More sharing options...
fugix Posted April 27, 2011 Share Posted April 27, 2011 yeah with the mysql_num_rows...it basically tells you how many rows of data your query grabbed..so if there isn't any data that matches your query...it will return 0 Quote Link to comment https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207055 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.