PHP Populating Combo Box with data from a table

Posted on 2009-12-22
Last Modified: 2013-12-12
I have a combo box and need to populate it with data from a database.
How do i do that?
Question by:funkkube
    LVL 15

    Accepted Solution

    if you have for example table 'combovalues', that contains column 'combovalue', then this way:

    <select name="comboname">
      $result = mysql_query('select * from combovalues');
      while ($row = mysql_fetch_assoc($result)) {
       echo '<option value="'.$row['combovalue'].'">'.$row['combovalue'].'</option>';

    (not tested in real enviroment)
    LVL 1

    Assisted Solution

    first of all... make a connection to the database
     $link = mysql_connect("localhost","root","pass") or die (mysql_error());
    $db = mysql_select_db("data_base") or die (mysql_error());

    $query = "SELECT * FROM countries";
    $sql  = mysql_query($query) or die (mysql_error());
    <!-- Building our form -->
    <form action="" method="post">
    Country List:
    <select name="country">
    while ($row = mysql_fetch_assoc($sql)){
      echo "<option value='".$row['id_country']."'>".$row['country_name']."</option>";
    <input type="submit" value="Send" />


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now