Jumat, 09 Oktober 2015

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

Bagikan

Jangan lewatkan

Php Code : How To Get Select Option Value From MySQL Database In Php
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.