tommyda Posted January 31, 2009 Share Posted January 31, 2009 The following code is giving output if there are results but not if there are no results $q2 = mysql_query("SELECT MAX(bid_amount) as 'maxbid' FROM bids WHERE bid_auction_id = '$auction_id'"); if (mysql_num_rows($q2) > 0){ $r2 = mysql_fetch_array($q2); echo $r2['maxbid'];} else{echo'no';} Can anyone see the problem? Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/ Share on other sites More sharing options...
Orionsbelter Posted January 31, 2009 Share Posted January 31, 2009 whats your url please Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/#findComment-751541 Share on other sites More sharing options...
Snart Posted January 31, 2009 Share Posted January 31, 2009 Most likely, the query is for some reason always returning a result even if the WHERE clause is not met, so (mysql_num_rows($q2) > 0) is always true. However, because the WHERE clause was not met, there is no 'maxbid' in the result and $r2['maxbid'] amounts to nothing. Try the following to test this: echo $r2['maxbid'].' - Test'; Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/#findComment-751543 Share on other sites More sharing options...
tommyda Posted January 31, 2009 Author Share Posted January 31, 2009 Most likely, the query is for some reason always returning a result even if the WHERE clause is not met, so (mysql_num_rows($q2) > 0) is always true. However, because the WHERE clause was not met, there is no 'maxbid' in the result and $r2['maxbid'] amounts to nothing. Try the following to test this: echo $r2['maxbid'].' - Test'; Thank you, you were right but how would I avoid this? Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/#findComment-751547 Share on other sites More sharing options...
premiso Posted January 31, 2009 Share Posted January 31, 2009 The reasoning for that is max will return "0" or nothing always if there is no data to be pulled. You cannot really avoid this, as far as I am aware. Do this instead: $q2 = mysql_query("SELECT MAX(bid_amount) as 'maxbid' FROM bids WHERE bid_auction_id = '$auction_id'"); $r2 = mysql_fetch_array($q2); if (!empty($r2['maxbid'])) { echo $r2['maxbid'] . " - Maxbid"; }else { echo 'There was no max bid for that auction'; } Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/#findComment-751554 Share on other sites More sharing options...
tommyda Posted January 31, 2009 Author Share Posted January 31, 2009 Tahnks Quote Link to comment https://forums.phpfreaks.com/topic/143300-solved-help-with-if-else-statement/#findComment-751573 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.