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 4 of 4
  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Display MySQL Data In A Form

    Hi All,

    I am having some trouble with using the MySQL "SELECT" statement to retrieve one COLUMN of data and then placing it into a select form input....

    My current syntax is below but I am not sure how I would insert the variables into the form once I select them from the database.... ps. a loop statement would be nice to repeatedly select until there is nothing left...

    Code:
    <?php
    $result = mysql_query("SELECT username FROM users");
    echo "<option>";
    echo "";
    echo "</option>";
    ?>
    Any help is greatly appreciated!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Hi,

    Loop the results with mysql_fetch_assoc();
    PHP Code:
    <?php
        $result 
    mysql_query("SELECT username FROM users");
        echo 
    '<select name="users">';
        while(
    $row mysql_fetch_assoc($result))
        {
            echo 
    '<option value="' $row['username'] . '">' $row['username'] . '</option>';
        }
        echo 
    '</select>';
    ?>
    Ref:
    http://us2.php.net/control_structures
    http://us2.php.net/mysql_fetch_assoc

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    wow! thanks - works like a charm... but what would I need to add to have the "select" options show in alphabetical order by username?

    thxs
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $result = mysql_query("SELECT username FROM users ORDER BY username ASC");


  •  

    Posting Permissions

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