Front page, PHP and MySQL database character encoding (to solve the problem of Chinese and other garbled characters)

Source: Internet
Author: User
Tags php and mysql

Web development often involves the interaction of data between front-end Web pages--php--mysql, but when the data is only English, there is usually no problem, but once the Chinese language is involved, the character encoding in three places is inconsistent (for example, when the Web page is used GBK and MySQL uses utf-8) It is possible to cause garbled characters to appear.

(Note: For character encoding see Baidu Encyclopedia: Http://baike.baidu.com/view/1204863.htm?fr=aladdin)

Front page encoding:

Usually we think we can pass the <meta> item in

This situation involves the header section of the HTTP protocol communication, in fact, when a user browses a Web page, the content that the server sends to the user not only includes our web pages (including Html/css/js), but also includes descriptive content called headers. This tells the client the type of data to be received (HTML or plain text or multimedia files, etc.), size, source, and so on (if you want to look at this information, you can use the Telnet tool (instead of using the browser) to initiate a GET request by itself using the HTTP protocol). Because the head is a priority over HTML sent,<meta> as part of the HTML its priority is also lower than the head, if the header already contains a description of the character encoding of the Web page, the browser will eventually be described in the header of the character encoding set to parse the page.

In PHP, you can use the header ("content-type:text/html; Charset=xxx "); To send a header about the character set.

For the Apache server, it has a adddefaultcharset function, that is, each sent to the Web page according to the server default character set the corresponding header.

View/etc/apache2/httpd.conf (before 2.4) or/etc/apache2/conf-available/charset.conf (2.4 and later), with a sentence adddefaultcharset xxx If this sentence is not in the comment state, then the ability to add a default character set header for each page is turned on, and the character set in the individual settings <meta> tag is not effective.

Note: HTML pages should be encoded in the same way that they are encoded when you actually write HTML pages (which are actually plain text).

In general, in order to be compatible with Chinese and even more languages, using UTF-8 encoding is the easiest way to do this, because utf-8 almost all of the world's most commonly used languages.

MySQL Database encoding:

After the terminal mysql-uusername-ppassword into the MySQL control program, and then type show variables like ' character% '; (Note that typing a command statement or a SQL statement in the MySQL terminal can not be omitted), you can see something like:

It lists the character sets that MySQL uses at various levels, where (*)

Character-set-server/default-character-set: The server character set, which is used by default.
Character-set-database: Database character set.
Character-set-table: Database table character Set.
Priority is increased in turn. So, in general, you only need to set character-set-server, and you do not specifically specify a character set when creating databases and tables, so that the Character-set-server character set is used uniformly.
Character-set-client: The character set of the client. The client default character set. When a client sends a request to the server, the request is encoded in that character set.
Character-set-results: Result character Set. When the server returns a result or information to the client, the result is encoded in that character set.
On the client, if Character-set-results is not defined, the Character-set-client character set is used as the default character set. So only the character-set-client character set is required.

So we will find that the character-set-server shown is not using UTF8 (note: in MySQL, Utf-8 encoding is expressed as UTF8, without "-"). This is because the default mode of storage for MySQL is latin1 without modification. In this case, when we use the MySQL terminal to create the database and the data table operation, if the SQL statement does not specify the character set used, then the encoding used by the store will be latin1, obviously, the Chinese stored in this would have been used to store Latin encoding way to display it must be garbled.

So how to modify it, you can use set character-set-server = UTF8, (due to the high character-set-server priority, only modify it to modify the database storage encoding effect). After that, the table is stored and encoded in the terminal using SQL Utf-8.

However, this modification is only valid for the current service, using quit, and entering the MySQL terminal again after exiting will find the character set back to Latin1. A method that you want to make the modification permanent in effect the information I found now shows that only by recompiling MySQL when modifying the compilation parameters to achieve, if there is a master know how not through the compilation implementation please message to inform.

PHP Code:

So, when it comes to how mysql,php is going to interact with MySQL, how do you ensure that the data is not garbled during the transfer?

As described in (*), in fact, in order to make the data stored in MySQL and removed from MySQL is not garbled, we just make the following three system parameters set to the same character set as the server character set Character-set-server. They are:
Character_set_client: The character set of the client.
Character_set_results: Result character Set.
Character_set_connection: The connection character set.
Setting these three system parameters can be accomplished by sending a statement to MySQL: set names xxx (xxx can be UTF8)

Therefore, when it comes to sending non-English characters, such as Chinese, from PHP to MySQL, use mysql_query ("Set names UTF8") after the Mysql_connect statement, and the statement (which assumes that the database is stored with UTF8) can safely transmit and retrieve Chinese.

Also, since HTML pages can actually be generated dynamically by PHP, how do you ensure that PHP dynamically generated pages are encoded in the same way as headers or <meta>?

Find the php.ini file in the PHP directory modified default_charset = "Utf-8" can make PHP in the output page using Utf-8 to encode.

Reference:

Http://zhidao.baidu.com/link?url=u6hNAXaLxOYEcBIViascG2fgPRorVYQyN9eULCiVyNCKihlqUGJmCBOZWtp6KS8DO7UbWuCr3GeodvukDJLdQ_

Http://niutuku.com/tech/Mysql/237673.shtml

Front page, PHP and MySQL database character encoding (to solve the problem of Chinese and other garbled characters)

Related Article

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.