How Can I Add an ELSE to My WHILE Loop?

Discussion in 'PHP' started by Masterful, Sep 15, 2008.

  1. #1
    Below is a simplified segment of my code. How can I add an ELSE to it?

    $sql = "SELECT * FROM tadv, tcat, tc 
    WHERE tc.advid=tadv.advid 
    AND tc.catid=tcat.catid"; 
    
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
       
    while($row = mysql_fetch_assoc($result))
    
    {
    echo "<table>";
    echo "<tr>";
    echo "<td>" . $row['one'] . "</td>";
    echo "<td>" . $row['two'] . "</td>";
    echo "</tr>";
    echo "</table>";
    }
    PHP:
    I tried the following but it didn't work:

    $sql = "SELECT * FROM tadv, tcat, tc 
    WHERE tc.advid=tadv.advid 
    AND tc.catid=tcat.catid"; 
    
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);  
       
    while($row = mysql_fetch_assoc($result)) 
    {
    if(!$row) 
    {
    echo "No matches found!";
    }
    else 
    {
    echo "<table>";
    echo "<tr>";
    echo "<td>" . $row['one'] . "</td>";
    echo "<td>" . $row['two'] . "</td>";
    echo "</tr>";
    echo "</table>";
    }
    }
    PHP:
    Any help will be rewarded with reputation points. :)
     
    Masterful, Sep 15, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
    $sql = "SELECT * FROM tadv, tcat, tc
    WHERE tc.advid=tadv.advid
    AND tc.catid=tcat.catid";
    
    $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); 
    
    if(mysql_num_rows($result) < 1){
    
    
    echo "No matches found!";
    
    }else{
       
    while($row = mysql_fetch_assoc($result))
    {
    
    echo "<table>";
    echo "<tr>";
    echo "<td>" . $row['one'] . "</td>";
    echo "<td>" . $row['two'] . "</td>";
    echo "</tr>";
    echo "</table>";
    }
    }
    PHP:
     
    php-lover, Sep 15, 2008 IP
    Masterful likes this.
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks, PHP-lover! It worked. ;)

    As promised, I added points to your account. :)
     
    Masterful, Sep 15, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    :) I'm glad it's work, thanks for the rep..
     
    php-lover, Sep 15, 2008 IP