smidgen11 Posted April 12, 2011 Share Posted April 12, 2011 I know this is probably something very dumb but I am having trouble with my if else. I have a sql query that looks for a record and if nothing is found it echos a warning. <?php $sql7="SELECT * FROM `$tbl_name2` WHERE Mac = '$Mac' AND Type = '$Type'"; $result7=mysql_query($sql7); if($result7) { while($row=mysql_fetch_array($result7)) echo "The phone you are using with mac $row[0] $row[1] is a Cisco IP Phone! <br>" ; } else { echo "<img src=\"stop.png\"/> The phone you are trying to use with mac $row[0] $row[1] is not a correct Mac\Type. <br> Please confirm the mac and type again."; } ?> I believe my syntax is correct as my if echo works fine. However, when no records are found I do not see my last else echo. Any input? Quote Link to comment https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/ Share on other sites More sharing options...
Maq Posted April 12, 2011 Share Posted April 12, 2011 You're not checking for the amount of results. Change this line to: if(mysql_num_rows($result7) > 0) Look up the returned values for mysql_query. The only time you will get to your else is if your query returns a FALSE on error (and maybe some other cases). Quote Link to comment https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/#findComment-1200714 Share on other sites More sharing options...
PFMaBiSmAd Posted April 12, 2011 Share Posted April 12, 2011 A query that executes without any errors, returns a result resource. That result resource may contain zero rows. You would use mysql_num_rows to test how many rows were matched. A query that results in an error will return a FALSE value. Your code is only testing if the query executed with or without an error. Quote Link to comment https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/#findComment-1200717 Share on other sites More sharing options...
smidgen11 Posted April 12, 2011 Author Share Posted April 12, 2011 awsome.....that did it... thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/#findComment-1200721 Share on other sites More sharing options...
Maq Posted April 12, 2011 Share Posted April 12, 2011 In the future, please use tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/#findComment-1200722 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.