What is the recommended encoding for PHP databases and output to avoid character conversion issues?
When working with databases in PHP, it is recommended to use UTF-8 encoding to avoid character conversion issues. This ensures that special characters and symbols are stored and displayed correctly. Additionally, when outputting data to the browser, using functions like htmlentities() can help prevent any potential encoding problems.
// Set UTF-8 encoding for database connection
$pdo = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password');
// Output data with htmlentities to avoid encoding issues
echo htmlentities($row['column_name'], ENT_QUOTES, 'UTF-8');