Coding Fun Html How to Create an HTML Select Box Populated With Data From a MySQL Database Using PHP

How to Create an HTML Select Box Populated With Data From a MySQL Database Using PHP





Full source code available at:

In this video you’ll learn how to use PHP to query data from a database and use it to populate an HTML select box.

-~-~~-~~~-~~-~-
Please watch: “Ryan Carson: How to Get an IT Job WITHOUT a College Degree”

-~-~~-~~~-~~-~-
#php #webdev

Tags: , , , , , , , , , , , , ,

37 thoughts on “How to Create an HTML Select Box Populated With Data From a MySQL Database Using PHP”

  1. kimani h says:

    Thanks bruv. 8 years later and it still works. Thank you

  2. Melle C says:

    This is a really good video! Thanks alot!

  3. how would one separate php code and html code? i mean, what should i do if i want to call .php script from another file, and have access to the data from html file?

  4. what is the value of var $option ??

  5. Muniru Bawa says:

    @John Morris thank you for the all the good work on the videos . i need to know how to grab the ids of the values in the select option to use those ids to query a statement from the database again. if you can help me i will forever thankful 

    the options id is what i need to grab and return that to database to query the second statement

    thank you

  6. Uses this its newer and uses sqli

    <?php
    $servername = "";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT Name, ID FROM name ORDER BY Name";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    echo "<select>";
    while($row = $result->fetch_assoc()) {
    echo "<option value='" . $row['Name'] . "'>" . $row['Name of column in table']. ", " . $row['spare column if needed ']. "</option>";

    }
    echo "</select>";
    } else {
    echo "0 results";
    }
    $conn->close();
    ?>

  7. Bro… you are just a hero for me…..love your coding.. thank you very much

  8. SimonBaku says:

    Call to a member function mysqli_fetch_object() on a non-objec

  9. This is a wicked tutorial however I have used both the code in the tutorial as well as the code on your site and I keep running into the same issue.  I can get my webpage to display with the drop down list however it doesn't contain any values.  I've successfully connect to my mySQL database so it must be a problem with my code, specifically the latter section for the dropdown.  it looKs like this:

    <h3>Dropdown</h3>
    <select name= "the_name">
      <?php foreach($array as $option) : ?>
      <option value="<?php echo $option->first_name; ?>"> <?php echo $option->last_name; ?> </option>
      <?php endforeach; ?>
    </select>

    I can't figure it out for the life of me 🙁

  10. David Ashby says:

    Expanding on this tutorial how would you go about populating a second drop down depending on your selection from dropdown1? thanks

  11. hey.. i need ur help.. do u have any tutorial that has a compelete code where it stores data from text. check box and drop down lists? thank  :* .. i wiill really appreciate it 

  12. How would you get the value of the drop down list and display it in another file?

  13. Jamal Taylor says:

    Hey John, just subscribed, how would I change this to radio buttons?

  14. no john, all 5 drop down list are in the same page,after selecting 1st list the data of 2nd list appears automatically.. so on 3rd and….like country,state and city module in registration form….although i have done it by myself and its working now..thanx for your time anyway….will ask your help if needed in future.

  15. hi John,actually i wanted to do somthng else. i have 4-5 drop-down lists in my page its values are coming from db,based on selected value from list. next drop down will automatically select its value based on previous.when user clicks on GET.he should get filtered data.i'm getting data when i fire simple query but it doesn't give anythng when i put where clause like[select * from data where b='$b' and c='$c' and..] how can i do it ??plz mail me any info- tprateek1191@gmail.com.thanx in advance,

  16. dashbyictfd says:

    I now have 2 dropdown boxes, 1 dependent on the other. They are populated from a MySQL DB, now I have 2 textareas underneath, these will draw data from same table dependant on combobox2 selection. Can you give me some suggestions as to how I will acomplish this please?

  17. John Morris says:

    Ok, so you'd have the first drop-down on a page. A user would select an option from that drop-down and hit submit. That data would be posted to a second page where you grab the $_POST array and find what they selected on the first page and use that to query your DB and populate your second drop-down on this second page.

  18. dashbyictfd says:

    So if I wasn't too worried about being on same page I could do it in php? as long as I am able to populate the second drop down on the selection of the first I would be happy with that, for now anyway.

  19. John Morris says:

    If you want it to all happen on the same page without any kind of refresh… yes. PHP is server-side so there's no way for it to know what dropdown is selected. Only a client-side language like JavaScript can do that.

  20. dashbyictfd says:

    Is that even when using just 1 MySQL table that holds everything. Topic and sub topics? Thanks

  21. John Morris says:

    You'd need JavaScript for that kind of thing. You could filter the second drop-down based on what's selected in the first.

  22. dashbyictfd says:

    How would you go about making a 2nd drop down act depending on the choice from the first dropdown. I have a db that populates the dropdown but depending on the selection I want it to go back to the same table and populate another dropdown with all the options under the heading selected from the 1st drop down. Thanks for any help

  23. how can i use the selected value?

  24. Colombia : amigo como lleno un comboBox html desde base de datos Access

  25. John Morris says:

    @msantanau Thanks dude!

  26. John Morris says:

    @rubynav Thanks! Glad you enjoyed it!

  27. John Morris says:

    @rubynav Thanks and no problem. Glad you enjoyed it!

  28. Pablito M. says:

    Great vid…I'm learning PHP and your tips really helps. Thanks for uploading.

  29. John Morris says:

    @CJTCarter No problem. Glad you found it useful.

  30. Very Awesome! Thanks dude

Leave a Reply

Your email address will not be published. Required fields are marked *