How can PHP be used to fill dropdown boxes with data from a database?

To fill dropdown boxes with data from a database using PHP, you can query the database to retrieve the data you want to display in the dropdown. Then, loop through the results and create option elements for each item in the dropdown.

<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to get data from database
$sql = "SELECT id, name FROM table";
$result = $conn->query($sql);

// Create dropdown
echo "<select>";
while($row = $result->fetch_assoc()) {
    echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";

// Close connection
$conn->close();
?>