dropdown data from database php code example

Example 1: get data from database in dropdown list php

<?php
$connect = new mysqli("127.0.0.1","root","");

			if ($connect -> connect_errno)
				{
			  		echo "Failed to connect to MySQL: " . $connect -> connect_error;
			  		exit();
				}

					$connect -> select_db("Your Database");
				if ($result = $connect -> query("Your query")) 
  				{
			  		
				}
				echo "<select>";
				while($row = mysqli_fetch_array($result)) 
				{
					echo "<option>" . $row['Your columns'] . "</option>"
				}
				echo "</select>"
?>

Example 2: drop down php form retrieve data from database

<!DOCTYPE html>
<html>
<head>
  <title>PHP Retrieve Data from MySQL using Drop Down Menu</title>
</head>
<body>

<form>
  City:
  <select>
    <option disabled selected>-- Select City --</option>
    <?php
        include "dbConn.php";  // Using database connection file here
        $records = mysqli_query($db, "SELECT city_name From tblcity");  // Use select query here 

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

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

</body>
</html>

Tags:

Php Example