Php garbled problem solution

Source: Internet
Author: User
Recently, I was trained by BBT to build a voting system. The system code is not very difficult, but I spent most of my time studying character sets and encoding, encoding of MySQL and Apache systems (character

Recently, I was trained by BBT to build a voting system. The system code is not very difficult, but I spent most of my time studying character sets and encoding, the encoding (character set) problem of MySQL and Apache makes me have a hard time thinking, and I am online to solve these problems.

The solution is scattered and one-sided. most of the solutions are provided, but I don't want to explain why. so I will summarize the gains over the past few days to avoid future detours, this article is a little helpful for PHP writing (you will know after reading it, how to make your PHP programs display normally on servers of most space providers ), however, more help lies in the setup and setup of network servers.

Let's talk about the MySQL character set. in Windows, you can modify the code in my. ini:

  1. # CLIENT SECTION
  2. [Mysql]
  3. Default-character-set = utf8
  4. # SERVER SECTION
  5. [Mysqld]
  6. Default-character-set = utf8

These two fields are used to change the default character set of the database. The first is the default character set of the client, and the second is the default character set of the server. suppose we set both fields to utf8, then enter "show variebles like" character_set _ % ";" in the MySQL Command Line Client. The following characters are displayed:

  1. Character_set_client latin1
  2. Character_set_connection latin1
  3. Character_set_database utf8
  4. Character_set_results latin1
  5. Character_set_server utf8
  6. Character_set_system utf8

The utf8 changes with the above settings, at this time, if we read data from the database through the PHP program using UTF-8, it is likely to be a string of "???" Or other garbled characters. after checking the data online for half a day, the solution is simple. after connecting to the database and reading the data, run a query item "set names UTF8" first ", the following code is used in PHP:

Mysql_query ("set names UTF8 ");

The display is normal. as long as the characters in the database information are normal, why? In this query, what is the function of "set names UTF8?

Enter "set names UTF8;" in the MySQL command line, and then execute "show variebles like" character_set _ % ";",

The original latin1 variables "character_set_client", "character_set_connection", and "character_set_results" are all changed to utf8. it turns out that these three variables are in the box and read the manual, the above sentence is equal:

  1. SET character_set_client = utf8;
  2. SET character_set_results = utf8;
  3. SET character_set_connection = utf8;

Let's take a look at the functions of the three variables:

Information input path: client → connection → server;

Information output path: server → connection → results.

In other words, each path must undergo three character set encoding changes. in the case of garbled output, for example, the utf8 data in the server, the incoming connection is converted to latin1, and the incoming results is converted to latin1, the UTF-8 page transfers results again. if the two character sets are incompatible, such as latin1 and utf8, the conversion process is irreversible and destructive, so it cannot be switched back.

However, we need to declare that "set names UTF8" is only temporary, and the default value is restored after MySQL is restarted. next we will talk about the configuration of MySQL on the server, isn't it because we have to add "SET NAMESUTF8" to each database read/write to ensure data transmission encoding consistency? Can we configure MySQL to achieve the three variables which are the character set we want by default? I did not mention this in the manual, and I did not find the answer on the internet. Therefore, from the server configuration perspective, there is no way to omit that line of code.

Summary:Add "set names UTF8" to display your webpage on more servers, even if you do not add this sentence.

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.