What best practices should be followed when assigning values to radio buttons based on database query results in PHP?

When assigning values to radio buttons based on database query results in PHP, it is important to ensure that the values are properly sanitized to prevent SQL injection attacks. Additionally, it is recommended to use prepared statements to securely retrieve data from the database. Finally, make sure to loop through the query results and assign the values to the radio buttons accordingly.

// Assume $conn is the database connection object

// Prepare a SQL query to retrieve the data
$stmt = $conn->prepare("SELECT id, option_name FROM options_table");
$stmt->execute();

// Bind the results to variables
$stmt->bind_result($id, $optionName);

// Loop through the results and create radio buttons
while ($stmt->fetch()) {
    echo '<input type="radio" name="option" value="' . htmlspecialchars($id) . '">' . htmlspecialchars($optionName) . '<br>';
}

// Close the statement
$stmt->close();