What are the common pitfalls or issues related to encoding and character sets when working with PHP and MySQL databases?

One common issue related to encoding and character sets when working with PHP and MySQL databases is that data can be stored or retrieved incorrectly if the character sets are not properly configured. To solve this issue, it is important to ensure that the character sets of both the PHP application and the MySQL database are set to the same encoding, such as UTF-8.

// Set the character set for PHP
mysqli_set_charset($connection, 'utf8');

// Set the character set for MySQL database
mysqli_query($connection, 'SET NAMES utf8');