Welcome to WebmasterWorld Guest from 54.162.151.77

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to check if query-results are empty?

Check if query could find data or not

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

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Dec 8, 2004
posts:500
votes: 0


$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'
}
8:51 pm on Oct 30, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 19, 2005
posts:96
votes: 0


Thank you very much for the help, dmmh! It works perfectly now!
11:09 am on Oct 31, 2005 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 8, 2004
posts:500
votes: 0


welcome :)