envexlabs Posted August 14, 2007 Share Posted August 14, 2007 Hey, I have a query: SELECT * FROM `table` WHERE `ID` = 5; If the query doesn't return anything, ie. ID #5 doesn't exist, how do i check that? Thanks, envex Quote Link to comment https://forums.phpfreaks.com/topic/64881-solved-checking-to-see-if-select-returns-nothing/ Share on other sites More sharing options...
wildteen88 Posted August 14, 2007 Share Posted August 14, 2007 You'd use the PHP function called mysql_num_rows, this function will return how many results have been returned from the query, eg: $sql = 'SELECT * FROM `table` WHERE `ID` = 5'; $result = mysql_query($sql); // check to see if the returned rows is greater than zero if(mysql_num_rows($result) > 0) { // result has been returned // process the results } else { // no results was returned, we'll display a message echo 'No results returned'; } Quote Link to comment https://forums.phpfreaks.com/topic/64881-solved-checking-to-see-if-select-returns-nothing/#findComment-323735 Share on other sites More sharing options...
SnowControl Posted August 14, 2007 Share Posted August 14, 2007 mysql_num_rows($result) Counts the number of rows used. Or: mysql_affected_rows() Same result. So: if (mysql_affected_rows()==0) { echo "This rows doesnt exist"; } Quote Link to comment https://forums.phpfreaks.com/topic/64881-solved-checking-to-see-if-select-returns-nothing/#findComment-323736 Share on other sites More sharing options...
envexlabs Posted August 14, 2007 Author Share Posted August 14, 2007 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/64881-solved-checking-to-see-if-select-returns-nothing/#findComment-323742 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.