Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if mysql entry is empty in PhP?

Tags:

php

mysql

record

here is the description variable I am echoing from my table:

$description = mysql_result($result,$i,"description");

sometimes the $i'th record is empty and doesn't have any data in it/no description.

What I want to do is echo "No description available" for records that are empty

if (isset($description)){ echo "No description available";}
else{ echo $desctipion;}

My attempt doesn't work though as it then echoes No description available for every record even those which aren't empty.

What is the answer?

like image 879
David Willis Avatar asked Oct 19 '09 23:10

David Willis


1 Answers

isset($description) will result true because $description is still set, even though its value is 'empty'. What you need to use is empty.

if (empty($description)) {
    echo "No description available";
} else {
    echo $description;
}
like image 108
Matt Huggins Avatar answered Sep 18 '22 16:09

Matt Huggins