Forum Moderators: coopster
I'm trying to do something that is probably really easy.
What im doing is extracting something from a database, but if nothing is there to extract, then it echos a message "nothing in the database".
I've tried variations on this theme, including isset, is_null, swapping the statement order around etc etc...I've got it showing both the data, and the no hegdehogs, but not at the right times, or nothing at all...
$data = mysql_query ("SELECT * FROM database WHERE animals='hedgehogs' ORDER BY id DESC")
or die (mysql_error());
while ($info = mysql_fetch_array( $data ))
if (empty($data)) {
echo "<p>No hedgehogs currently live here...</p>";
}
else
{
echo "<p>This is a: " .$info['animals'] . "</p>";
}
where am i going wrong?
thanks
:)
I think the problem is that sometimes a field is 'Null', sometimes it is 'empty' and sometimes it contains a blank string.
This is a problem I have quite often, especially with CMS based sites.
The way I usually go about it is to check for a few different eventualities such as:
if (empty($data) ¦¦ is_null($data) ¦¦ $data == '') {
echo "<p>No hedgehogs currently live here...</p>";
}
else
{
echo "<p>This is a: " .$info['animals'] . "</p>";
}
I don't know whether the things I'm checking for are one and the same but this solution usually works for me.