PDA

View Full Version : How to display some content if database gives no results



remp
07-03-2010, 05:36 PM
I have a simple script that should get information from a database and it works fine, but im trying to get it to display something else when there is nothing to display from the database... i have tryied a few thing but i can't seem to get it to do so.... can someone give me a hand? please. It is greatly appreciated...


The following code seems to work when there is no data to display, but it also displays the text when there is something there... how can i fix this?



$query = "SELECT * FROM information ORDER BY date, time";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
if ($row['date'] >= date('Y-m-d'))
{
displayEvents($row);
}
}

if($row['id']){
displayEvents($row);
}

else{
echo "there is nothing to display";
}

djr33
07-03-2010, 07:05 PM
Basically what you need is something like "while, else" and that doesn't exist.
The easiest way to do this is to use a variable to remember:

while (...) {
...
$results = TRUE;
}
if ($results!==TRUE) {
....
}


You could do it a few ways, but that should work.


The reason it currently doesn't work is because you are checking if there's a value in $row, when mysql_fetch_assoc() has just returned false (always): either because there were no results (1st time, false) or after all the results are done (last time, false).

remp
07-03-2010, 07:16 PM
i have changed it to this, but like i said, it still shows the text when there is the information to display.


while($row = mysql_fetch_assoc($result))
{
if ($row['date'] >= date('Y-m-d'))
{
displayEvents($row);

$results = TRUE;
}
}

if ($results!==FALSE) {
echo "there is nothing to display";
}

?>

djr33
07-03-2010, 07:20 PM
You wrote "FALSE". It should be "TRUE".
Also, the better way to do it might be:
if (!isset($results)) {.....

remp
07-03-2010, 07:26 PM
Oops! i see.... I have made the according changes and it works now. Thank alot for your help. i really appreciate it.