Home > Backend Development > PHP Tutorial > goodreader garbled code two ways to solve php garbled problem

goodreader garbled code two ways to solve php garbled problem

WBOY
Release: 2016-07-29 08:48:21
Original
2238 people have browsed it

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 :

 二招解决php乱码问题

2, use mysq_query to set the encoding when connecting to the database with php
Syntax: mysql_query("SET NAMES 'utf8'");
For example:

Copy the code The code is as follows:


$c
mysql_query("set names 'utf8'",$conn); //Solve garbled characters
mysql_select_db('test1',$conn);


Generally, as long as you do these two steps, your There will be no garbled characters in the program.
Second general situation:
The second general situation generally does not occur often. It mostly occurs when you are working on a variety of programming languages. For example, sometimes you make utf8, sometimes you make gb2312, and sometimes you make big5 web pages. , caused by the difference between the encoding of the .php file created with the IDE and the encoding to be displayed by the program.
For example, 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 The encoding used is gb2312
Take my commonly used IDE Dreamweaver as an example

 二招解决php乱码问题

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.
If you still can’t figure it out, then add a line of http header information to the head of the web page
header("Content-Type: text/html; charset=utf -8");
Note: When sending header information, there cannot be any output in front of the header, including spaces.
Okay, now that I have written so much, let’s take a look at which situation your program is causing garbled code.

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template