What are common issues with character encoding and PHP when working with databases?

Common issues with character encoding in PHP when working with databases include garbled text, special characters not displaying correctly, and data corruption. To solve this, it is important to ensure that the character encoding settings are consistent across the database, PHP, and the HTML output.

// Set character encoding for database connection
mysqli_set_charset($conn, 'utf8');

// Set character encoding for HTML output
header('Content-Type: text/html; charset=utf-8');

// Set character encoding for PHP
ini_set('default_charset', 'UTF-8');