bschultz Posted May 30, 2011 Share Posted May 30, 2011 I'm trying to come up with some code...and need to echo certain things based on which query failed. There are several query's and while loops nested together. I'm trying to use this... <?php $rs = mysql_query($sql,$dbc); if (! $rs) { echo "You aren't scheduled to work in the next seven days...but check back often as the schedule is adjusted often."; } $matches = 0; while ($row = mysql_fetch_assoc($rs)) { $matches++; //rest of code here...which work when there are matches... ?> With no matches, it isn't echoing anything...instead of "You aren't scheduled to work in the next seven days...but check back often as the schedule is adjusted often." If I echo out $rs, I get RESOURCE ID #6 Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/237926-no-matches-problem/ Share on other sites More sharing options...
Drummin Posted May 30, 2011 Share Posted May 30, 2011 Try if (empty($rs)) or without space if (!$rs) Quote Link to comment https://forums.phpfreaks.com/topic/237926-no-matches-problem/#findComment-1222610 Share on other sites More sharing options...
bschultz Posted May 30, 2011 Author Share Posted May 30, 2011 no difference...still doesn't echo anything Quote Link to comment https://forums.phpfreaks.com/topic/237926-no-matches-problem/#findComment-1222612 Share on other sites More sharing options...
mikesta707 Posted May 31, 2011 Share Posted May 31, 2011 well it would help if you provided the query you are using also, but I will assume you are using a select statement. With select statements, the result resource that is obtained from doing mysql_query will coerce to true unless there was a mysql error (even if there are no rows returned). You basically want to check if there were any rows returned. You can use mysql_num_rows to check how many rows were returned from your query. for example $rs = mysql_query($sql,$dbc); $num = mysql_num_rows($rs); if (!$num) { echo "You aren't scheduled to work in the next seven days...but check back often as the schedule is adjusted often."; } $matches = $num;//no need for a while loop Quote Link to comment https://forums.phpfreaks.com/topic/237926-no-matches-problem/#findComment-1222655 Share on other sites More sharing options...
bschultz Posted May 31, 2011 Author Share Posted May 31, 2011 thanks...that worked! Quote Link to comment https://forums.phpfreaks.com/topic/237926-no-matches-problem/#findComment-1222681 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.