If else. MySQL Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

If else. MySQL Data

Post by techkid »

Code: Select all

<?php
include("config.php");
 
$result = mysql_query("SELECT * FROM events
WHERE upcoming='yes'");
 
if ($result){
    while($row = mysql_fetch_array($result))
  {
  echo $row['name'] . " " . $row['description'];
  }
}  
 
else{
 echo "no upcoming events";
}
 
?>
This doesn't work the way I want. I want display all the records with upcoming=yes. It works. and if there is no record i want to echo no upcoming events. It displays a blank page insread of echo "no upcoming events"; . Any other way to do this job?
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: If else. MySQL Data

Post by synical21 »

Personally i would create an exception, i dont know if that is the best method you will probably find better ones here :)

Code: Select all

 
 
$qry = ("SELECT * FROM events
WHERE upcoming='yes'");
$result = mysql_query($qry);
 
try {
        if (!mysql_num_rows($result)) {
         throw new Exception("We have no results");
       }
 
else{
 
$qry = mysql_query("SELECT * FROM events
WHERE upcoming='yes'");
 
while($row = mysql_fetch_array($qry))
{
echo $row['name'] . " " . $row['description'];
}
}
} catch (Exception $e) {
     
    // The exception has returned 0
    echo "no upcoming events.";
 
}
 
Post Reply