PHP + MYSQL for website development usually encounters garbled characters when the browser outputs Chinese characters. this problem is mainly caused by HTML content encoding. PHP + MYSQL for website development usually encounters garbled characters when the browser outputs Chinese characters, this problem is mainly caused by inconsistent HTML content encoding, php file encoding, and MySQL database encoding. Next we take UTF-8 as an example to briefly describe how to unify the relationship between the three.
Add a php file named test_charset.php and save the following code to the file:
Hello,
! Your email is: ">
HTML content encoding
The above row 22nd:
Here we specify that the browser uses UTF-8 encoding when parsing HTML. If this parameter is not specified, the browser uses its own default encoding. The default encoding varies with browsers, for example, IE6 is GB2312 and FireFox is UTF-8. Therefore, if there are no 22nd lines of the above code, the code will be displayed normally in FireFox and garbled characters will be displayed in IE6.
Php file encoding
Php files also need to be encoded in the same way. How can I check the encoding of my php file? In Windows, Notepad can be used for processing. Open the php file in Notepad and select "Save As..." from the "file" menu, for example:
There is an encoding option at the bottom of the displayed dialog box. The current encoding of the file is displayed. To change to another encoding, select from the drop-down list box and click "save.
MySQL database encoding
After the database connection is successful, you should immediately execute a set encoding command, such as the first line of the above code. Here a little need to pay special attention, UTF-8 is a normal way of writing, but in MySQL into UTF8, there is no crossline in the middle. The above 7th lines set the connection encoding to utf8 ($ charset = "utf8") instead of UTF-8. The complete implementation of ECShop connection encoding is attached for your reference. The file is des/cls_mysql.php.
Function set_mysql_charset ($ charset) {/* if the mysql version is 4.1 + or later, you need to initialize the character set */if ($ this-> version> '4. 1 ') {if (in_array (strtolower ($ charset), array ('gbk', 'big5', 'utf-8', 'utf8 '))) {$ charset = str_replace ('-', '', $ charset);} if ($ charset! = 'Latin1') {mysql_query ("SET character_set_connection = $ charset, character_set_results = $ charset, character_set_client = binary", $ this-> link_id );}}}