Post Date: May 08, 2017
Last Updated: September 28, 2026
Introduction
A dynamic dropdown list allows users to select an option from a list of values retrieved from a database. In this post, we will explain how to create a dynamic dropdown list using HTML, PHP, and MySQL. The example demonstrates how to retrieve country names from a MySQL table and display them dynamically within an HTML <select> element.
Prerequisites
Before creating the dynamic dropdown list, ensure the following requirements are available:
- A web server such as Apache or Nginx.
- PHP installed and configured on the server.
- MySQL database server.
- Basic knowledge of HTML, PHP, and MySQL.
- A MySQL database with permission to create tables and execute queries.
- A PHP file such as
dropdown.phpto implement the dynamic dropdown list.
Implementation
Step 1: Create a new file, dropdown.php
Step 2: Create MySQL connection code inside dropdown.php
$mysqlserver="localhost";
$mysqlusername="root";
$mysqlpassword="";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'test';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
Step 3: Create a MySQL table ls_countries
CREATE TABLE `ls_countries` ( `country_id` int(11) NOT NULL AUTO_INCREMENT, `sortname` varchar(3) NOT NULL, `name` varchar(150) NOT NULL, `phonecode` int(11) NOT NULL, `status` int(11) NOT NULL COMMENT '1->active,0->deactive', PRIMARY KEY (`country_id`)) ENGINE=InnoDB AUTO_INCREMENT=247 DEFAULT CHARSET=utf8
Step 4: Create a drop-down list dynamically using the following code
<select>
<option>Select Country</option>
<?php
$cdquery="SELECT * FROM ls_countries";
$cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());
while ($cdrow=mysql_fetch_array($cdresult)) {
$cdTitle=$cdrow["name"];
echo "<option>$cdTitle </option>";
}
?>
</select>
We are following the above steps and getting code like this
<?php
$mysqlserver="localhost";
$mysqlusername="root";
$mysqlpassword="";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = 'linus';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
?>
<!DOCTYPE html>
<html>
<head>
<title>Drop Down Using Php</title>
</head>
<body>
<select>
<option>Select Country</option>
<?php
$cdquery="SELECT * FROM ls_countries";
$cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());
while ($cdrow=mysql_fetch_array($cdresult)) {
$cdTitle=$cdrow["name"];
echo "<option>$cdTitle </option>";
}
?>
</select>
</body>
</html>
Conclusion
In this post, we have explained how to create a dynamic dropdown list using HTML, PHP, and MySQL. The PHP code connects to the MySQL database, retrieves country names from the ls_countries table, and dynamically generates the dropdown options. This approach can be extended to populate dropdown lists with other database-driven values such as states, cities, categories, or products.
FAQs
1. How can I create a dynamic dropdown list using PHP?
You can retrieve the required values from a MySQL database using PHP and generate <option> elements dynamically inside an HTML <select> element.
2. Can I populate a dropdown list with data from a MySQL database?
Yes. PHP can execute a MySQL query, fetch the records, and display the retrieved values as dropdown options.
3. Can the dynamic dropdown list be used for states, cities, or categories?
Yes. The same approach can be used to dynamically populate dropdown lists with states, cities, categories, products, or any other database records.
Related Articles
How to Disable/Lock a MySQL User Account from CLI
Talk to our experts
Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.