jedispyder Posted May 6, 2009 Share Posted May 6, 2009 I have a message I'm trying to return when I get a null result after doing a query, but can't seem to get it to work. I think the problem is finding out which variable to use to test when something is null or if I'm just testing it wrong. My code: //$result is the result of the query if ($result != "") { while($row = mysql_fetch_array($result)) { echo "Results returned."; } } else { echo "Sorry, but no results were returned."; } It properly returns results when something is returned, but if nothing is returned then it doesn't return the "Sorry, but not results were returned." Another code I tried that still didn't work was: if (!$result) { echo "Sorry, but no results were returned."; } I've tried including it within the While and using $row but still haven't had any luck. Any help? Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/ Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Wrong forum. This should be in PHP Help. Read mysql_query. It doesn't return a null value, ever! Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827154 Share on other sites More sharing options...
Maq Posted May 6, 2009 Share Posted May 6, 2009 Try: if ($result) { Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827182 Share on other sites More sharing options...
jedispyder Posted May 6, 2009 Author Share Posted May 6, 2009 Ken2k7 > Sorry for putting it in the wrong forum, I saw other posts on problems with "If" here and thought this would be the right place. Sorry! Maq > I tried using that and it didn't work =o/ And I did look up mysql_query and tried several of those combos but none worked. Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827192 Share on other sites More sharing options...
BioBob Posted May 6, 2009 Share Posted May 6, 2009 Dont use "if ($result != "")", that checks its mysql recordset, not the actual data you want. So even if there are no results, you'll still get a recordset back. instead after you do your query, try this: if (mysql_num_rows($sql) > 0) { //code... } Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827215 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Can you post more code? Perhaps you have a SQL error? Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827240 Share on other sites More sharing options...
jedispyder Posted May 6, 2009 Author Share Posted May 6, 2009 Dont use "if ($result != "")", that checks its mysql recordset, not the actual data you want. So even if there are no results, you'll still get a recordset back. instead after you do your query, try this: if (mysql_num_rows($sql) > 0) { //code... } Thanks, it works perfectly now! Link to comment https://forums.phpfreaks.com/topic/157017-solved-problem-with-if-else-from-null-queries/#findComment-827322 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.