How to display MySQL data in Drop Down List using Php


To display MySQL table data in Drop Down List we need to mix the Php script with HTML. See  the below example how to perform this.

Suppose this is the SQL table with some data
State_Name
State_Code
Jammu and Kashmir
01
Punjab
02
Gujarat
03
Madhya  Pradesh
04
Mumbai
05
Goa
06
Jharkhand
07
Orissa
08
Chennai
09
Andhra Pradesh
10
Jharkhand
11
Bihar
12
West Bengal
13

Now i will show the state name in Drop Down List

<?php
@mysql_connect('localhost','root','') or die('check username and password');
@mysql_select_db('record') or die('not connected');
?>
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
<style type="text/css">
table
{
 width:50em;
}
fieldset
{
 width:20em;
 background-color:lightgreen;
}
</style>
</head>
<body bgcolor="lavender">
<div>
<center>
<fieldset>
<legend><font size="5px" color="blue"></font></legend>
<select name="state_name">
<option>--Select your State--</option>
<?php
$query = mysql_query("select name from employee");
while($result = mysql_fetch_array($query))

?>
<option><?php echo $result['state_name']; ?> </option>
<?php } ?>
</select>
</fieldset>
</center>
</div>
</body>
</html>