Domesticated Brain

Fetching data from a MYSQL database and inserting them into a HTML drop-down list.

In this topic we fetch date from the table tblcourse in a database called domesticatedbrain and load them into the HTML dropdown list

To create a drop down list in HTML we have to use select element in HTML and the option tag which will define the available options in the dropdown list to select with.

Here is the simple example to create a drop down list in html with few options to select.

 

<html>

 <select>

  <option value=" computer_basics "> Computer Basics</option>

  <option value=" computer_networks ">Computer Networks </option>

  <option value=" web_engineering ">Web Engineering </option>

  <option value=" database_systems "> Database Systems</option>

 </select>

</html>

 

The HTML drop-down list we created shown in the web browser as follows

Insert Data Into a Drop-down Out Put

Now we are going fetch data from the column named CourseTitle in our tblcourse table and load them in to a drop-down menu using PHP programming language.

First you have to create a connection to the database server and the required data base

Following PHP code example shows how to open a database connection for MySQL database

 

<?php

$db_con = mysql_connect("localhost","root","root");

    if (!$db_con){

                     die( "Database connection failed" . mysql_error());

                }else{

                                $db = mysql_select_db("domesticatedbrain",$db_con);

                                if(!$db){

                                  die("cann't select the required database". mysql_error());

                               

                               }else{               

                               }

                }

?>

 

Now let’s find out how we can include data in the database table called tblcourse into our HTML drop-down list

The following piece of code will do the job.

<select>
<?php
     $records = mysql_query("SELECT CourseTitle FROM tblcourse ");
     while ($row = mysql_fetch_array($records)){
     echo "<option value=\"\">" . $row['CourseTitle'] . "</option>";
    }
?>
</select>

 

If you have found  what you expected please comment and like on our facebook page