Forum Moderators: coopster

Message Too Old, No Replies

How to check if query-results are empty?

Check if query could find data or not

         

dbzfyam

5:05 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



I have never tried this before, but is there a way I can find out if a query does not return any data (with other words: if it can't find any data)?
I have a page with links to other sites stored in a MySQL database in various categories. If a category is empty, my script does not display anything. I want it to display a message that no data was found.

Does anyone know how to do this?

Thanks in advance,
Stefan

dmmh

6:56 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



$query= 'SELECT * FROM table'." WHERE id IS NOT NULL";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0){
echo $row['category'];
}else{
echo 'no category'
}

dbzfyam

8:51 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



Thank you very much for the help, dmmh! It works perfectly now!

dmmh

11:09 am on Oct 31, 2005 (gmt 0)

10+ Year Member



welcome :)