• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Home
  • About Us
  • Django
  • Laravel
  • Interview Questions
  • Tips

CodingStatus

- Learn Coding to Build Web Applications

  • JavaScript
  • jQuery
  • ReactJS
  • Ajax
  • Node.js
  • PHP
  • SQL

Display Data From Database in Select Options using PHP & MYSQL

March 26, 2022 By Md Nurullah Leave a Comment

In this tutorial, You will learn to display data from the database in select options using PHP & MySQL with some simple steps. These steps are very easy to understand and implement in web applications.

Here, I have shared source code to display data in a single dropdown select option. Once you learn it, you will easily customize it according to your project requirement.php display data in select option

Contents

  • How to Fetch Data From Database in PHP and Display in Select Option
    • 1. Insert Select Option Value
    • 2. Connect Database to display data
    • 3. Fetch Data From Database
    • 4. Display Data in Select Option

How to Fetch Data From Database in PHP and Display in Select Option

Before getting started it’s coding, you should create the following folder structure –

codingstatus/
     |__database.php
     |__ fetch-data.php
     |__ display-data.php
     |

Learn Also –

Delete Multiple Records with checkboxes in PHP & MySQL

Preview Images before uploading using PHP

Create PHP Image Gallery 

1. Insert Select Option Value

To display data from the database in a select option value, First of all, You will have to insert the select option value into the database. So, Make sure, you have already done it.

2. Connect Database to display data

To insert a select option value in the database, you must connect PHP to MySQL database with the help of the following query.

Where –

  • $hostName – It must contain hostname.
  • $userName – It must contain username of the database.
  • $password – It must contain password of the database
  • $database – It must contain database name.

File Name – database.php

<?php
$hostName = "localhost";
$userName = "root";
$password = "";
$databaseName = "codingstatus";
 $conn = new mysqli($hostName, $userName, $password, $databaseName);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
?>

3. Fetch Data From Database

To fetch data from database, you will have to implement the following steps –

Step-1:  Write SQL query to select from the “course”

Step-2: store option data in the $options by fetching from the database

File Name – fetch-data.php

<?php 
    $query ="SELECT courseName FROM courses";
    $result = $conn->query($query);
    if($result->num_rows> 0){
    	$options= mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
?>

4. Display Data in Select Option

To display data in select option, you will have to follow the following steps –

Step-1: First of all, Include database.php and then also include the fetch-data.php

Step-2: Apply foreach loop to the $option and print the option value within select option input field.

File Name – display-data.php

<?php
include("database.php");
include("fetch-data.php);
?>
<select name="courseName">
   <option>Select Course</option>
  <?php 
  foreach ($options as $option) {
  ?>
    <option><?php echo $option['courseName']; ?> </option>
    <?php 
    }
   ?>
</select>

 

Related Posts:

  • PHP Interview Questions with Answers 2021
  • Javascript Interview Questions and Answers for Freshers &…
  • How to print PHP Array
  • React props with Examples

Filed Under: PHP

Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India. I'm a Software Engineer. I have been working in the Web Technology field for 3 years. Here, I blog about Web Development & Designing. Even I help developers to build the best Web Applications.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Follow us

  • facebook
  • twitter
  • linkedin

Recent Posts

  • SQL Join 3 Tables – Join Query for Three Tables
  • Load Records on Select Box using Ajax, PHP & MySQL
  • Display data based on dropdown selection in PHP & MySQL
  • Edit and Update Dropdown Value in PHP & MySQL
  • Display Data From Database in Select Options using PHP & MYSQL
  • Home
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • Sitemap
  • Contact Us

Copyright © 2022 CodingStatus - All Rights Reserved