How can a PHP script be modified to prevent multiple form outputs when populating select boxes with multiple entries from a database?

When populating select boxes with multiple entries from a database in a PHP script, the issue of multiple form outputs can be resolved by using an associative array to store the database entries and then looping through the array to generate the select options. By doing so, each entry will be displayed only once in the select box.

// Connect to database and fetch entries
$entries = array();
$query = "SELECT id, name FROM entries";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
    $entries[$row['id']] = $row['name'];
}

// Generate select box options
echo "<select name='entry'>";
foreach ($entries as $id => $name) {
    echo "<option value='$id'>$name</option>";
}
echo "</select>";