SQL If/Else

Discussion in 'Coding Help' started by Nugget, Nov 1, 2011.

Tags:
  1. Nugget

    Nugget New Member

    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. php.allstar

    php.allstar New Member

    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. Nugget

    Nugget New Member

    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
     
  4. php.allstar

    php.allstar New Member

    You're welcome, good luck with it now.
     

Share This Page