How to fill a dropdown combo box in PHP from MySQL database table

This is an easy to use code to select records from MySQL database table and display in dropdown combo box using PHP.

$cn=mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());
mysql_select_db($db_name,$cn) or die(mysql_error());
$sql = "SELECT field_name FROM table_name";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select>";
while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["field_name"]."'>".$row["field_name"]."</option>";
}mysql_free_result($rs);
echo "</select>";

Leave a Reply

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


5 + one =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>