What are best practices for handling character encoding and BOM in PHP when interacting with databases like MySQL?

When interacting with databases like MySQL in PHP, it's important to ensure that character encoding is handled properly to avoid issues with data integrity. One common issue is the presence of a Byte Order Mark (BOM) at the beginning of the data, which can cause problems when inserting or retrieving data from the database. To handle this, it's recommended to set the character encoding to UTF-8 and remove any BOM before interacting with the database.

// Set the character encoding to UTF-8
mysqli_set_charset($connection, 'utf8');

// Remove BOM from input data
$input_data = preg_replace('/\x{FEFF}/u', '', $input_data);

// Insert data into the database
$query = "INSERT INTO table_name (column_name) VALUES ('$input_data')";
mysqli_query($connection, $query);

// Retrieve data from the database
$query = "SELECT column_name FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$output_data = $row['column_name'];

// Output the data with proper encoding
echo utf8_encode($output_data);