What are the best practices for ensuring proper encoding and collation settings when updating values in a MySQL database with PHP?

When updating values in a MySQL database with PHP, it is important to ensure that the encoding and collation settings are properly set to avoid any data corruption or unexpected behavior. To do this, you can explicitly set the character set and collation for the database connection before executing any queries.

// Set the character set and collation for the database connection
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8mb4");
$mysqli->query("SET collation_connection = 'utf8mb4_unicode_ci'");

// Update values in the database
$query = "UPDATE table_name SET column_name = 'new_value' WHERE condition";
$mysqli->query($query);

// Close the database connection
$mysqli->close();