Page 1 of 1

If else. MySQL Data

Posted: Wed Dec 16, 2009 1:51 pm
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?

Re: If else. MySQL Data

Posted: Wed Dec 16, 2009 2:01 pm
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.";
 
}