Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    Steger, IL
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Populating Drop Down from MySQL using PHP

    I am trying to populate a drop down menu of MySQL data using PHP and I have hit a snag. I think its probably something simple that a freah pair of eyes could pick out right away that I am just not seeing... For some reason only the "firstname" part of the data is populating in the drop down and I can't figure out why. When I try to make changes to the echo lines to correct this, nothing shows up including the "firstname" data. The code is below...

    PHP Code:
    <?php

    //connect to MySQL

    $connect mysql_connect("localhost","xxx","xxx") or
    die (
    "Could not connect to database.");

    //choose the database

    mysql_select_db("clients");

    //get data from MySQL database

    $result mysql_query('SELECT clientid, firstname, lastname FROM people')
    or die (
    mysql_error());

    //populate drop down menu with data from MySQL database

    echo '<select name="clientlist">';
    while (
    $row mysql_fetch_assoc($result))
    {
    echo 
    '<option value="' $row['clientid'] . '">' $row['firstname']. '  ' $row['lasttname'] . '</option>';
    }
    echo 
    '</select>';

    ?>
    Thanks,

    Jim
    Jim

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    Why not solve it like this:

    Code:
    SELECT clientid, concat(firstname, ' ', lastname) as fullname FROM people
    PHP Code:
    echo '<option value="' $row['clientid'] . '">' $row['fullname']. '</option>'

  • #3
    New Coder
    Join Date
    Sep 2005
    Location
    Steger, IL
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I could do that I supposed, but that still doesn't explain why only the middle part of the whole section of code was appearing in the drop down. Does the drop down have a problem pulling INT value from the database or something like that perhaps, it doesn't make sense...
    Jim


  •  

    Posting Permissions

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