www.webdeveloper.com
Results 1 to 2 of 2

Thread: No Data

  1. #1
    Join Date
    Nov 2007
    Posts
    362

    No Data

    I have the following code, that outputs Firstnames and Lastnames from a table inside a database.

    How do I display a message if the database/table contains no Firstnames and Lastnames?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die(mysql_error());
    }
    
    mysql_select_db("db", $con);
    
    $result = mysql_query("SELECT * FROM person");
    
    while($row = mysql_fetch_array($result))
    {
      echo $row['FirstName'].'<br />';
      echo $row['LastName'].'<br />';
    }
    
    mysql_close($con);
    ?>

  2. #2
    Join Date
    May 2007
    Posts
    205
    put what you get from the DB into variables and check to see if they have anything in them:

    PHP Code:
    <?

    $firstname
    =$row['FirstName'];
    $lastname=$row['LastName'];

    //now check your variables
    if($firstname!='' && $lastname!=''){

    ?>
    <div>
    First Name: <?= $firstname ?><br>
    Last Name: <?= $lastname ?><br>
    </div>

    <? } else { ?>
    <div>
    Oops! No First and last names found in the DB.
    </div>

    <? ?>
    or something like that

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles