Home >Backend Development >PHP Tutorial >goodreader garbled code two ways to solve php garbled problem
Garbled characters on PHP web pages are generally caused by the difference between the encoding used when creating the database and the encoding of the PHP web page.
If you don’t specify the encoding for a database created with phpmyadmin, it defaults to latin1_swedish_ci encoding. Swedish is not case-sensitive, and we in China The web pages you make are either GBK or GB2312 encoded, so it would be strange if there are no garbled characters.
1. Specify the encoding when creating the database.
I will talk about the commonly used encodings here to avoid confusion for novices:
If you are making Simplified Chinese For web pages, then you use GB2312 encoding when creating a database, gb2312_chinese_ci.
If you are making a traditional Chinese web page, then you need to use gib5 encoding when building a database, big5_chinese_ci
If the web pages you are making have both simplified and traditional Chinese, then I recommend you Use GBK encoding, gbk_chinese_ci. GBK contains more character codes than GB2312, and of course traditional Chinese is also included.
If you are making a multi-language web page, it is recommended that you use UTF-8 encoding. There are two kinds of utf8 encoding available in mysql Choice: utf8_unicode_ci and utf8_general_ci. The author generally uses utf8_general_ci. For the difference between these two encodings, please refer to another article on this site: The difference between utf8_unicode_ci and utf8_general_ci in the proofreading set in Mysql
We use UTF-8 encoding as an example to create a database as shown in the figure :
2, use mysq_query to set the encoding when connecting to the database with php
Copy the code The code is as follows:
$c
mysql_query("set names 'utf8'",$conn); //Solve garbled characters
mysql_select_db('test1',$conn);
The new webpage created by IDE is gb2312, but I changed the built webpage to big5. How can there be no garbled code in this way? , the solution is very simple, just save it as again, specify the encoding and it will be OK.
The above introduces the goodreader garbled code and two methods to solve the php garbled problem, including the content of goodreader garbled code. I hope it will be helpful to friends who are interested in PHP tutorials.