How can PHP be used to populate dropdown menus with values from a database?
To populate dropdown menus with values from a database using PHP, you can query the database to retrieve the values and then loop through the results to generate the options for the dropdown menu. This can be achieved by using PHP to generate HTML code for the dropdown menu with the values retrieved from the database.
<?php
// Connect to the database
$connection = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Query to retrieve values from the database
$query = "SELECT id, name FROM table_name";
$result = mysqli_query($connection, $query);
// Generate dropdown menu options
echo "<select>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
// Close database connection
mysqli_close($connection);
?>