Should SET NAMES UTF8 Be Used for Unicode Support?
In the esteemed book "High performance MySQL," author Baron Schwartz warns against using "SET NAMES UTF8" as it incorrectly alters server settings instead of modifying the client library's character set. This has prompted confusion among developers who have relied on this syntax for Unicode support.
Best Practices for Unicode-Aware Workflow
To ensure a Unicode-aware database workflow, consider the following best practices:
1. Use mysql_set_charset() / mysqli_set_charset() for PHP:
These functions directly set the character set for the client connection, bypassing the limitations of "SET NAMES UTF8."
2. Specify Connection Parameters for PDO:
PDO::mysql supports a "charset" parameter to specify the character set during connection initialization.
3. Set MySQL Server Variables:
For optimal performance, set the following server variables in the my.ini/cnf configuration:
character_set_client = utf8 character_set_results = utf8 character_set_connection = utf8
4. Caution Regarding Multiple Applications:
Be aware that altering these server variables might affect other applications running on the same MySQL instance that require a different character set.
The above is the detailed content of Should I Use `SET NAMES UTF8` for Unicode Support in MySQL?. For more information, please follow other related articles on the PHP Chinese website!