Two tricks to solve php garbled issues

Source: Internet
Author: User
PHP garbled text has been mentioned N + 1 times, but it is still common for new users to know how to solve the php garbled text problem. I will repeat it here, I hope it will be helpful for new users. php web page garbled characters are generally caused by different encoding used in 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:

The 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.