Home  >  Article  >  Backend Development  >  Two ways to solve the php garbled problem_PHP tutorial

Two ways to solve the php garbled problem_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:57900browse

Garbled characters in 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 do not specify the encoding for the database created with phpmyadmin, the default encoding is latin1_swedish_ci, which means Swedish is not size-sensitive. Write, and the web pages we make in China 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.

Here again Let me talk about the commonly used encodings to avoid confusion for novices:

If you are making a simplified Chinese web page, then you use GB2312 encoding, gb2312_chinese_ci when building the database.

If you are doing It is a traditional Chinese web page, so when you create the database, you must use gib5 encoding, big5_chinese_ci

If the web page you make has both simplified and traditional Chinese, it is recommended that you use GBK encoding, gbk_chinese_ci. GBK contains more character codes than GB2312 Many, 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 utf8 encoding options in mysql: utf8_unicode_ci and utf8_general_ci. The author generally uses utf8_general_ci , Regarding the difference between these two encodings, please refer to another article on this site: The difference between the proofreading set utf8_unicode_ci and utf8_general_ci 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
Syntax: mysql_query("SET NAMES 'utf8'");
For example:
Copy code The code is as follows:

$conn=mysql_connect('127.0.0.1','root ','123456');
mysql_query("set names 'utf8'",$conn); //Solving garbled characters
mysql_select_db('test1',$conn);

Under normal circumstances, as long as you do these two steps, your program will not have garbled characters
Second general situation:
The second general situation generally does not occur much, and mostly occurs when doing There are many programming languages. For example, sometimes we make utf8, sometimes we make gb2312, and sometimes we make big5 web pages. The encoding of the .php file created with the IDE is different from the encoding to be displayed by the program.
For example, I Last time I made a gb2312 simplified Chinese web page, and now my boss has asked me to make a gbi5 web page, but my IDE uses the gb2312 encoding
Take my commonly used IDE Dreamweaver as an example

The new web page created by IDE is gb2312, but I changed the built web page to big5. How can there be no garbled characters? The solution is very simple. Save it as again and specify Just encode it and it’s OK.

If you still can’t figure it out, add a line of http header information to the head of the web page

header("Content-Type: text/html; charset=utf-8" ; The garbled code may be caused by the situation.



http://www.bkjia.com/PHPjc/325214.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/325214.htmlTechArticleGarbled characters on the php web page are generally caused by the difference between the encoding used when creating the database and the encoding of the php web page. Use phpmyadmin If you do not specify the encoding of the created database, it defaults to latin1_swedis...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn