How can PHP developers effectively determine if a record set contains data before displaying dropdown menus to prevent empty selections?

To prevent empty dropdown menus, PHP developers can check if the record set contains data before populating the dropdown options. This can be done by checking the number of rows returned by the query. If the record set is empty, the dropdown menu should not be displayed.

// Assuming $result is the record set obtained from the database query

if ($result && $result->num_rows > 0) {
    // Display dropdown menu and populate options
    while ($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
    }
} else {
    // Display a message indicating no data is available
    echo "<option disabled selected>No data available</option>";
}