Advanced Search

Results 1 to 5 of 5

Thread: How to display some content if database gives no results

  1. #1
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to display some content if database gives no results

    I have a simple script that should get information from a database and it works fine, but im trying to get it to display something else when there is nothing to display from the database... i have tryied a few thing but i can't seem to get it to do so.... can someone give me a hand? please. It is greatly appreciated...


    The following code seems to work when there is no data to display, but it also displays the text when there is something there... how can i fix this?

    PHP Code:
    $query  "SELECT * FROM information ORDER BY date, time";
    $result mysql_query($query);

    while(
    $row mysql_fetch_assoc($result))
    {
        if (
    $row['date'] >= date('Y-m-d'))
        {
    displayEvents($row);
        }
    }

    if(
    $row['id']){
    displayEvents($row);
    }

    else{
    echo 
    "there is nothing to display";
     } 

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Basically what you need is something like "while, else" and that doesn't exist.
    The easiest way to do this is to use a variable to remember:

    while (...) {
    ...
    $results = TRUE;
    }
    if ($results!==TRUE) {
    ....
    }


    You could do it a few ways, but that should work.


    The reason it currently doesn't work is because you are checking if there's a value in $row, when mysql_fetch_assoc() has just returned false (always): either because there were no results (1st time, false) or after all the results are done (last time, false).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    i have changed it to this, but like i said, it still shows the text when there is the information to display.

    PHP Code:
    while($row mysql_fetch_assoc($result))
    {
        if (
    $row['date'] >= date('Y-m-d'))
        {
    displayEvents($row);

    $results TRUE;
        }
    }

    if (
    $results!==FALSE) {
     echo 
    "there is nothing to display";
    }

    ?> 

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    You wrote "FALSE". It should be "TRUE".
    Also, the better way to do it might be:
    if (!isset($results)) {.....
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2007
    Posts
    149
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Oops! i see.... I have made the according changes and it works now. Thank alot for your help. i really appreciate it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •