how can i check to see if data exists before pulling it out?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
suthie
Forum Commoner
Posts: 68
Joined: Sat Jun 09, 2007 10:46 am

how can i check to see if data exists before pulling it out?

Post by suthie »

I use this code to get data from a database:

Code: Select all

$sql = "SELECT * from dailyvibe WHERE userlink='$username' ORDER BY postid DESC";
$result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)){     
$date = stripslashes($row['timestamp']);    
$title = stripslashes($row['title']);    
$entry = stripslashes($row['entry']);

...other stuff...

}
How can i make it so if there is no data with the correct username, it gives a message saying no data was found?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

if (!mysql_num_rows($result))
    echo "No data found.";
Post Reply