Php reads the summary of mysql garbled text. if you need it, refer to it. When php reads mysql, the character set is involved in the following aspects.
1. specify the character set of the database table when creating the database table. For example
- Create table tablename
- (
- Id int not null auto_increment,
- Title varchar (20) not null,
- Primary key ('id ')
- ) Default charset = UTF8;
2. mysql character set Mysql has three important variables: character_set_client, character_set_results, and character_set_connection. Set character_set_client to tell Mysql how PHP is encoded in the database. Set character_set_results to tell Mysql what encoding data PHP needs. Set character_set_connection to tell Mysql what encoding is used for text in PHP queries.
3. after connecting to the database, set the default character encoding used for inter-database character transmission. Use mysqli: set_charset () or mysqli: query ('set names utf8') to set the parameter. Use mysqli: set_charset (mysqli: set_charset) instead of "set names" whenever possible"PS:(Refer to this article)
- $ Db = new mysqli ('localhost', 'user', 'passwd', 'database _ name ');
- $ Db-> set_charset ('utf8 ');
Note that utf8 is not UTF-8 (The problem here is that both the database and php are encoded in a unified manner. However, if the mysqli: set_charset () function is not called, garbled characters still occur when reading data. Why ?) (In addition, set names utf8 is equivalent to the following three sentences: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; )4. character set used by the html page. Set in meta tag
-
5. character set used by php text files. In linux, you can use vim to open a file and enter : Set encoding View character sets used by Files
To ensure no garbled characters, ensure the encoding of the file itself. the encoding specified in HTML is consistent with the encoding (including character_set_client and character_set_results) that PHP tells Mysql. Use the mysqli: set_charset () function or "set names ".
For the problem following "3", I wrote several examples. after testing the link to the database, I set and do not set the result of the character set. Test Environment: Ubuntu 12.04, MySQL 5.5, and php 5.5.7. The result is as follows: (1) the character set of the database table is utf8, and the set names utf8 is not used Chinese characters can be inserted and read normally, but garbled characters are displayed in mysql.
(2) the character set of the database table is utf8, and set names utf8 is used. Properly Insert and read Chinese characters. mysql displays the correct information.
(3) if the character set of the database table is not utf8, use set names utf8 Mysql shows that reading is a question mark.
Refer: [1] go deep into Mysql character set settings. http://www.laruence.com/2008/01/05/12.html [2] coding problem of PHP and MYSQL. http://blog.csdn.net/martinkro/article/details/5352474 [3] a deep understanding of the difference between set names and mysql (I) _ set_charset. http://www.laruence.com/2010/04/12/1396.html [4] MySQL "set names xxx" character SET problem analysis-http://bbs.phpchina.com/thread-13861-1-1.html
|