SQL If/Else

Advert test
#1
Hi I'm trying to pull upcoming fixtures of a sports team from my database. I'm using the following code to get the data:

Code:
$sql = "SELECT * FROM `fixtures` WHERE `Date` > CURDATE()";

$result=mysql_query($sql);

$level=mysql_result($result,0,"Level");
$date=mysql_result($result,0,"Date");
$opponent=mysql_result($result,0,"Opponent");
$venue=mysql_result($result,0,"Venue");
$time=mysql_result($result,0,"Time");
$crest=mysql_result($result,0,"Crest");
?>
I was wondering how I would display a message saying there are no upcoming fixtures if this returns nothing?
 
#2
Hi,

You would do something along the lines of this:

PHP:
<?php

$sql = "SELECT * FROM `fixtures` WHERE `Date` > CURDATE()";

$result=mysql_query($sql);

if(mysql_num_rows($result) > 0){
    
    $hasFixtures = true;
    $level=mysql_result($result,0,"Level");
    $date=mysql_result($result,0,"Date");
    $opponent=mysql_result($result,0,"Opponent");
    $venue=mysql_result($result,0,"Venue");
    $time=mysql_result($result,0,"Time");
    $crest=mysql_result($result,0,"Crest");
    
} else {

    $hasFixtures = false;
        
}

?>
<?php if($hasFixtures): ?>
    Put your html fixture list in here
<?php else : ?>
    There are no upcoming fixtures
<?php endif; ?>
 
#3
Thanks a million php.allstar, that's exactly what I wanted. I just wasn't sure what the if statement should have been looking for. Works perfectly now
 
Top