Home >Backend Development >PHP Problem >php Chinese characters garbled
Solution to garbled Chinese characters in php:
1. The problem of garbled Chinese characters in pure PHP pages (the data is static )
If your PHP page is garbled, just add the following code at the beginning of the page.
<?php header("content-type:text/html;charset=utf-8"); //设置编码 ?>
2. PHP Mysql Chinese garbled problem
In addition to following the third operation, you must also add database encoding before querying/modifying/adding your data. Moreover, it is worth noting that the UTF8 here is different from the previous one, there is no horizontal line in the middle.
<?php mysql_query('SET NAMES UTF8'); //接下来的就是查出数据或者修改,增加 ?>
If the MySQL version you are using is 4.1 or higher, you can set a character encoding after the link database operation, like the following
UTF-8 encoding is just one of the encodings , if you don’t want to use UTF-8 encoding, you can also use other encodings. Just replace UTF-8 with the encoding you want to use. Currently, GB2312 and UTF-8 are mainly used in Chinese website development.
One thing to note: the mysql_query("set names 'coding'"); encoding added before the PHP program that needs to perform database operations must be consistent with the PHP encoding. If the PHP encoding is gb2312, then the mysql encoding It is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of php Chinese characters garbled. For more information, please follow other related articles on the PHP Chinese website!