how to fetch data from database in dropdown list in php?

Today, We want to share with you how to fetch data from database in dropdownlist in php?.In this post we will show you want to fetch data from database based on dropdown list selection using php, hear for how to retrieve data from database whenever select in drop down list in javascript we will give you demo and example for implement.In this post, we will learn about Jquery Ajax Dropdown Onchange Example In Php MySQL Database with an example.

Read Also:  Laravel 6 Multiple Where Eloquent Query

how to show data in dropdownlist from database in php?

Step 1: Connection with Database

config.php

<?php

$link = mysqli_connect("localhost","root","[email protected]","products_data");

if(!$link)
{
    die("Connection failed: " . mysqli_connect_error());
}

?>

Step 2: Fetch data from database

Fetch data from database and display in drop down list
index.php

<!DOCTYPE html>
<html>
<head>
  <title>PHP Retrieve Data from MySQL using Drop Down Menu - www.pakainfo.com</title>
</head>
<body>

<form>
  Category:
  <select>
    <option disabled selected>-- Select Category --</option>
    <?php
        include "config.php";  // Using database connection file here
        $products_cat = mysqli_query($link, "SELECT cat_name From categories");  // Use select query here 

        while($product_categoty = mysqli_fetch_array($products_cat))
        {
            echo "<option value='". $product_categoty['cat_name'] ."'>" .$product_categoty['cat_name'] ."</option>";  // displaying data in option menu
        }	
    ?>  
  </select>
</form>

<?php mysqli_close($db);  // close the Database connection ?>

</body>
</html>

I hope you get an idea about php drop down list selected value from database.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or opinions about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.