Php Code : How To Get Select Option Value From MySQL Database In Php

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>




///////////////OUTPUT:



                     
php populate select option values from mysql
Php Select Option




Share this

Related Posts

Previous
Next Post »

9 comments

comments
21 février 2017 à 09:07 delete

great job...keep posting more tutorials n source code ....

Reply
avatar
27 septembre 2017 à 08:32 delete

Sir i am having space between option values....how to resolve it? please reply

Reply
avatar
20 décembre 2017 à 02:42 delete

Sir i want to grab the values from mysql populated dropdown

Reply
avatar
26 octobre 2018 à 07:25 delete

Hi Sir (php select option value from database) after i done this how can i inert that values into database..

Reply
avatar
26 octobre 2018 à 07:26 delete

Hi Sir (php select option value from database) after i done this how can i inert that values into database..

Reply
avatar
15 octobre 2020 à 23:50 delete

why cannot add condition in mysql query etc. "where"

Reply
avatar