Repinted: http://blog.csdn.net/class1/archive/2006/12/30/1469298.aspx To enable normal display of Your webpage on more servers, add "set names utf8" (you can select the appropriate encoding, such as gb2312 ), even if you do not add this sentence, you can access it normally. Let's talk about the character set of MySQL. In Windows, you can modify
- # Client Section
- [MySQL]
- Default-character-set = utf8
- # Server section
- [Mysqld]
- Default-character-set = utf8
These two fields are used to change the default Character Set of the database. The first is the default Character Set of the client, and the second is the default Character Set of the server. Suppose we set both to utf8, and then input "show variables like" character_set _ % "in the MySQL command line client." The following characters are displayed: Character_set_client Latin1 Character_set_connection Latin1 Character_set_database utf8 Character_set_results Latin1 Character_set_server utf8 Character_set_system utf8 The utf8 is changed with the above settings. At this point, if we read data from the database through the PHP program using the UTF-8, it is likely to be a string of Or other garbled characters. After checking for half a day on the Internet, the solution is simple. After connecting to the database and reading data, run a query "set names utf8", that is
- Mysql_query ("set names utf8"); (uppercase)
It can be displayed normally (as long as the characters in the database information are normal ). Why? In this query, what is the function of "set names utf8? Enter "set names utf8;" in the MySQL command line, and then run "show variables like" character_set _ % ";", the original Latin1 variables "character_set_client", "character_set_connection", and "character_set_results" are all changed to utf8. Refer to the Manual. The above sentence is equal:
- Set character_set_client = utf8;
- Set character_set_results = utf8;
- Set character_set_connection = utf8;
Let's take a look at the functions of the three variables: Information Input path: client → connection → server; Information output path: Server → connection → results. In other words, each path must undergo three character set encoding changes. Taking garbled output as an example, for utf8 data in the server, the incoming connection is converted to Latin1, the incoming results is converted to Latin1, And the UTF-8 page is converted to results. If the two character sets are incompatible, such as Latin1 and utf8, the conversion process is irreversible and destructive. So it cannot be turned back. However, we need to declare that "Set names utf8" is only temporary, and the default value will be restored after MySQL is restarted. Next we will talk about the configuration of MySQL on the server. Isn't it because we have to add "set names utf8" to each database read/write to ensure Data Transmission Encoding consistency? Can we configure MySQL to achieve the three variables which are the character set we want by default? I did not mention the manual, and I did not find the answer online. Therefore, the line of code cannot be omitted from the server configuration perspective. Summarize the following points: Make sure that the return value of select is consistent with the program code, that is, character_set_results is consistent with the program code. Ensure that the program code is consistent with the browser, that is, the program code is consistent with <meta http-equiv = "Content-Type" content = "text/html; charset =? "/> Consistent. |