Home > Enterprise >  How to create a drop down menu form containing all the databases stored in mysql using PHP?
How to create a drop down menu form containing all the databases stored in mysql using PHP?

Time:04-09

php

$db_conn = mysqli_connect('localhost','root','Password');
$result = mysqli_query($db_conn,"SHOW DATABASES"); 
while ($row = mysqli_fetch_array($result)) { 
echo $row[0]."<br>"; 

}

So far I have used the following lines to display the databases on my webpage. But i need to show the databases in the form of a drop down menu

CodePudding user response:

not sure how you structured your database and HTML, but you need to add a <select> element in your page and loop your result as options for this select

Example

<?php

$db_conn = mysqli_connect('localhost','root','Password');
$result = mysqli_query($db_conn,"SHOW DATABASES");
?>
<select>
<?php 
  while ($row = mysqli_fetch_array($result)) { 
    echo "<option>{$row[0]}</option>"; 
  }
?>
</select>

I hope you got the idea

CodePudding user response:

The best solution to this problem is to wrap your result in a list. of course, dropdowns are always in a list... so on your dropdown menu, find the list and iterate through the list of databases like the one below.

<?php

$db_conn = mysqli_connect('localhost','root','Password');
$result = mysqli_query($db_conn,"SHOW DATABASES");

while ($row = mysqli_fetch_array($result)) { 
?>

<li> <?php echo $row[0]; ?> </li>

<?php
}

?>
  • Related