Display Data Dynamically in Dropdown using Php MySQLi


 Display Data Dynamically in Dropdown using Php MySQLi
Display Data Dynamically in Dropdown using Php MySQLi, 
php Mysql Dropdown tutorial , 
Php MySQL  Dynamically Select  
How to Display value in Dropdown,
Retrieve data from database and display in dropdown,
how to Fetch Data in Dropdown using Php MySQL

 

In this Tutorial lets run how to fetch data from MySQL database and display using dropdown or Select Box . here we are using category table . where table contains two fields id and category

Use following steps 

  1. Create databse 
  2. Create Connection file i.e. db.php
  3. Create code i.e. index.php 



Lets Create MySQL Database Demo use Following Sql 


CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `category` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`id`,  `category`) VALUES
(1,'General'),
(2,'PHP'),
(3,'HTML'),
(4,'Tables'),
(5,'Functions'),
(6,'Variables'),
(7,'Forms');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `categories`
--
ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `categories`
--
ALTER TABLE `categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
COMMIT;


Database Connection file db.php

<?php
    $servername='localhost';
    $username='root';
    $password='';
    $dbname = "freecode";
    $conn=mysqli_connect($servername,$username,$password,"$dbname");
      if(!$conn){
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>









Create Index.php File copy paste following code 

index.php




<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Display Data Dynamically in  Dropdown using Php Mysqli </title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
html, body {
background-color: gray;
color: black;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h2 class="text-primary">Display Data Dynamically in Dropdown using Php Mysqli FreesSourcecode.in </h2>
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="CATEGORY-DROPDOWN"><b>Category </b></label>
<select class="form-control" id="category-dropdown">
<option value="">Select Category</option>
<?php
require_once "db.php";
$result = mysqli_query($conn,"SELECT * FROM categories ");
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row["category"];?></option>
<?php
}
?>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

 Display Data Dynamically in Dropdown using Php MySQLi

Display Data Dynamically in Dropdown using Php MySQLi, 

php Mysql Dropdown tutorial , 

Php MySQL  Dynamically Select  

How to Display value in Dropdown,
Retrieve data from database and display in dropdown,
how to Fetch Data in Dropdown using Php MySQ


Newest
Previous
Next Post »

Display Data Dynamically in Dropdown using Php MySQLi

 Display Data Dynamically in Dropdown using Php MySQLi Display Data Dynamically in Dropdown using Php MySQLi,  php Mysql Dropdown tutorial ,...

Article