Php web page garbled characters are generally caused by different encoding used during database creation and php Web page encoding,
If you do not specify the encoding for a database created using phpmyadmin, the default encoding is latin1_swedish_ci. Swedish is case insensitive, while the webpage we create in China is GBK or GB2312, in this case, no gibberish occurs.
1. Specify the encoding when creating the database.
Here, I would like to talk about the commonly used code, so the newbie is confused:
If you are using a simplified Chinese webpage, you must use GB2312 encoding and gb2312_chinese_ci when creating a database.
If you are using a traditional Chinese Web page, you must use the gib5 encoding and big5_chinese_ci when creating a database.
If your webpage is simplified or traditional Chinese, we recommend that you use GBK encoding. gbk_chinese_ci. GBK contains more characters than GB2312. Of course, traditional Chinese also exists.
If you are doing Web pages in multiple languages, we recommend that you use UTF-8 encoding. there are two types of utf8 encoding available in mysql: utf8_unicode_ci and utf8_general_ci I generally use utf8_general_ci. For the differences between the two encoding types, refer to another article on this site: differences between utf8_unicode_ci and utf8_general_ci in Mysql
We use UTF-8 coding as an example to establish a database:
2. Use mysq_query to set the encoding when connecting to the database using php.
Syntax: mysql_query ("set names 'utf8 '");
For example:Copy codeThe Code is as follows: $ conn = mysql_connect ('127. 0.0.1 ', 'root', '123 ');
Mysql_query ("set names 'utf8'", $ conn); // fix garbled characters
Mysql_select_db ('test1', $ conn );
Generally, as long as you have completed these two steps, your program will not be garbled.
Generally:
Generally, many programming languages are used, such as utf8, gb2312, and big5. PHP file encoding is different from the encoding to be displayed by the program.
For example, I made a gb2312 Simplified Chinese webpage last time. Now my boss has told me to create a gbi5 webpage, but my ide uses the gb2312 code.
Take my commonly used IDE Dreamweaver for example.
The webpage created by ide is gb2312, but I changed the webpage to big5. How can this problem be avoided? the solution is simple. Save it as a new one, specify the encoding and then click OK,
If the problem persists, add an http header to the webpage header.
Header ("Content-Type: text/html; charset = UTF-8 ");
Note: When sending the header information, no output is allowed before the header, including spaces.
Well, after writing so much, let's see what causes Garbled text in your program.