What potential pitfalls should be avoided when working with character encoding in PHP and MySQL?

When working with character encoding in PHP and MySQL, one potential pitfall to avoid is mismatched character sets between the two. Ensure that the character sets are consistent and properly configured in both PHP and MySQL to prevent data corruption or display issues.

// Set character encoding for PHP
mysqli_set_charset($connection, "utf8");

// Set character encoding for MySQL
mysqli_query($connection, "SET NAMES 'utf8'");
mysqli_query($connection, "SET CHARACTER SET 'utf8'");