PHP & MySQL & Apache Unified encoding

Source: Internet
Author: User

PHP & MySQL & Apache Unified encoding

When using PHP and MySQL database interaction, always encountered garbled problem. Either the data read from the database garbled, or can not be stored in the database, God annoying.

The original default encoding format is Latin1, when it is read out of the time garbled (╯-╰), and then change it to GBK, when the deposit is still a problem. Baidu, then decided to unify the code ~

Here is an example of a unified UTF8 code:

Environment: mysql5.5,apache2.4,php5.6, operating system windows

MySQL Unified encoding

These are the statements that look at MySQL's current encoded format:

SHOW VARIABLES like ' character_set_% '; SHOW VARIABLES like ' collation_% ';

The problem of garbled external access data is on this connection connection layer, the workaround is to execute the following sentence before sending the query:

SET NAMES ' UTF8 ';

Locate the MySQL My.ini file, open and modify:

[Mysql]default-character-set=utf8[mysqld]character-set-server=utf8

[client] segment plus default-character-set=utf8   OK. The query results are as follows:

mysql> show variables like  ' character_set_% '; +--------------------------+-------------- -------------------------------------------+| variable_name             | Value                                                      |+--------------------------+--------------------------------------------------------- +| character_set_client     | utf8                                                        | |  character_set_connection | utf8                                                       | |  character_set_database   | utf8                                                       | |  character_set_filesystem | binary                                                     | |  character_set_results    | utf8                                                       | |  character_set_server     | utf8                                                       | |  character_set_system     | utf8                                                       | |  character_sets_dir       | c:\program files\mysql\mysql  server 5.5\share\charsets\ |+--------------------------+-------------------------------------- -------------------+8 rows in set  (0.00 sec) mysql> show variables  like  ' collation_% '; +----------------------+-----------------+| variable_name         | value           |+------ ----------------+-----------------+| collation_connection | utf8_general_ci | |  collation_database   | utf8_general_ci | |  collation_server     | utf8_general_ci |+----------------------+-----------------+3 rows  in set  (0.00 SEC)

This does not have to be added one at a time before SQL is queried set names utf8; .

PHP Unified Code

Find and modify in php.ini:

Default_charset = "UTF-8"
Apache Unified Code

According to the online recommendation, in Apache configuration file httpd.conf, locate the line containing "AddLanguage" or "Addcharset", and add one line to the front of the line:

Adddefaultcharset Utf-8

Of course, I did not find "AddLanguage" or "Addcharset" in Apache2.4, I added it directly at the end of the httpd.conf file .

Here are some tips to find out:

  1. Develop a good habit of adding this line to each page:

    <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
  2. Modify Apache to AddDefaultCharset utf-8 AddDefaultCharset off turn off the default character set so that Apache can choose to use the character set based on meta information in the Web page.

  3. PHP Output Header: header(“Content-Type: text/html; charset=utf-8") All files are encoded in UTF-8 format.

  4. The best use of UTF8 without BOM format , as to its differences with the Utf8+bom, you can search for yourself ~

Note:

Even after the unified coding, it is important to note that your cmd is GBK, so from the execution of the SQL statement, such as when the table, you need to tell the MYSQL server, your client cmd->mysql clients using GBK code .

Set names GBK;

Otherwise it always thought your client code is also UTF8 code, direct your GBK code does not convert into the database, will appear error.

SET NAMES XXX function is to tell the server, my client is what code.


The level is limited, only writes here, please everybody must be enthusiastic to seek me the trouble ~

This article is from the "Zero4eva" blog, make sure to keep this source http://zero4eva.blog.51cto.com/7558298/1589465

PHP & MySQL & Apache Unified encoding

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.