Trying to understand conditional syntax

Discussion in 'PHP' started by Joobz, Jan 16, 2008.

  1. #1
    I have a search form that checks a database and I want it to display a message if there are no records returned.... here's what I have so far which outputs a blank page when there are no records found:

    
    while($arr = mysql_fetch_assoc($result)){
       extract($arr);
       if (!$arr) {
        echo "Sorry, there were no records in the database";
        exit;
    }
    echo "Here are your records" . $arr;
    
    PHP:
    I think I have the wrong syntax on line three:

      if (!$arr) {
    PHP:
    Not sure though....
     
    Joobz, Jan 16, 2008 IP
  2. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Yup.. you didn't close the opening curly brace ({) for the if statement.
     
    rkquest, Jan 16, 2008 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    try

    
    if (empty($arr)) {
    	echo "Sorry, there were no records in the database";
    	exit;
    }
    
    PHP:
     
    Kaizoku, Jan 16, 2008 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The if() is pointless since $arr will always be a valid array at that point in the while() loop.

    Instead, before the while() loop starts, do something like this:

    
    $sql = "select blahblah";
    $result = mysql_query($sql);
    if (!mysql_num_rows($result))
    {
       echo "Sorry, there were no records in the database";
       exit;
    }
    while ($arr = mysql_fetch_assoc($result))
    {
       do_amazing_things();
    }
    
    PHP:
     
    SmallPotatoes, Jan 17, 2008 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    
    if (!mysql_num_rows($result))
    {
       echo "Sorry, there were no records in the database";
       exit;
    }
    
    PHP:
    Should be

    
    if (@mysql_num_rows($result)==0)
    {
       echo "Sorry, there were no records in the database";
    }else{
    //do something
    }
    
    PHP:
     
    HuggyStudios, Jan 17, 2008 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    @ is evil. It's the cause of so many bugs. It should be removed from the language.

    If you want to be thorough and test separately for mysql_num_rows() returning false, that's great. But what you've provided gives the same results as what I provided, except that it masks errors. Any error in this context almost certainly points to something that should be dealt with, so masking them is a bad idea.
     
    SmallPotatoes, Jan 17, 2008 IP
  7. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So much to absorb, I will look into these examples further. I feel like I am closer to my next php breakthough thanks to the help from DP members.

    Another thing, what is the purpose of the @?
     
    Joobz, Jan 17, 2008 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If you put @ before a function call, then any non-fatal error from that function will not be displayed.
     
    SmallPotatoes, Jan 17, 2008 IP
  9. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #9
    Its best to mask the mysql_num_rows as if it returns a 0 it will state a error, therfore you do not want that to occur you want to display your own error like, No results found.
     
    HuggyStudios, Jan 17, 2008 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Huh? If there are no result rows, no error message will ever be displayed; it will just politely return 0. That is not an error condition and is not treated as such. Have you actually tried it? mysql_num_rows() only throws an error when there are real problems, like an invalid statement handle.
     
    SmallPotatoes, Jan 17, 2008 IP