Php Code To Populate Html Select Drop Down List With Values From MySQL Database
In this Php Tutorial we will Learn Two Method On How To Populate Html Select Box With Data From MySQL Database Table In Php .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
Php Source Code:
<?php
// php select option value from database
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "test_db";
// connect to mysql database
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql select query
$query = "SELECT * FROM `users`";
// for method 1
$result1 = mysqli_query($connect, $query);
// for method 2
$result2 = mysqli_query($connect, $query);
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP SELECT OPTIONS FROM DATABASE </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<!--Method One-->
<select>
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[1];?></option>
<?php endwhile;?>
</select>
<!-- Method Two -->
<select>
<?php echo $options;?>
</select>
</body>
</html>
9 comments
commentsthx
Replygreat job...keep posting more tutorials n source code ....
ReplySir i am having space between option values....how to resolve it? please reply
ReplyI want to grab the valu
ReplySir i want to grab the values from mysql populated dropdown
ReplyHi Sir (php select option value from database) after i done this how can i inert that values into database..
ReplyHi Sir (php select option value from database) after i done this how can i inert that values into database..
ReplyThank You sir
Replywhy cannot add condition in mysql query etc. "where"
Reply