How can PHP be used to dynamically populate a dropdown menu with database names?
To dynamically populate a dropdown menu with database names using PHP, you can first establish a connection to the database and retrieve the list of databases. Then, loop through the results and generate the dropdown options accordingly.
<?php
// Establish a connection to the database
$conn = mysqli_connect("localhost", "username", "password");
// Retrieve the list of databases
$result = mysqli_query($conn, "SHOW DATABASES");
// Generate the dropdown menu with database names
echo '<select name="databases">';
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['Database'] . '">' . $row['Database'] . '</option>';
}
echo '</select>';
// Close the database connection
mysqli_close($conn);
?>