Summary of mysql garbled characters read by php

Source: Internet
Author: User
Summary of mysql garbled characters read by php

Php reads the summary of mysql garbled text. if you need it, refer to it.


When php reads mysql, the character set is involved in the following aspects.

1. specify the character set of the database table when creating the database table. For example

  1. Create table tablename
  2. (
  3. Id int not null auto_increment,
  4. Title varchar (20) not null,
  5. Primary key ('id ')
  6. ) Default charset = UTF8;

2. mysql character set
Mysql has three important variables: character_set_client, character_set_results, and character_set_connection.
Set character_set_client to tell Mysql how PHP is encoded in the database.
Set character_set_results to tell Mysql what encoding data PHP needs.
Set character_set_connection to tell Mysql what encoding is used for text in PHP queries.

3. after connecting to the database, set the default character encoding used for inter-database character transmission.
Use mysqli: set_charset () or mysqli: query ('set names utf8') to set the parameter.
Use mysqli: set_charset (mysqli: set_charset) instead of "set names" whenever possible"PS:(Refer to this article)

  1. $ Db = new mysqli ('localhost', 'user', 'passwd', 'database _ name ');
  2. $ Db-> set_charset ('utf8 ');

Note that utf8 is not UTF-8
(The problem here is that both the database and php are encoded in a unified manner. However, if the mysqli: set_charset () function is not called, garbled characters still occur when reading data. Why ?)
(In addition, set names utf8 is equivalent to the following three sentences:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
)

4. character set used by the html page. Set in meta tag

5. character set used by php text files.
In linux, you can use vim to open a file and enter
: Set encoding
View character sets used by Files

To ensure no garbled characters, ensure the encoding of the file itself. the encoding specified in HTML is consistent with the encoding (including character_set_client and character_set_results) that PHP tells Mysql. Use the mysqli: set_charset () function or "set names ".


For the problem following "3", I wrote several examples. after testing the link to the database, I set and do not set the result of the character set. Test Environment: Ubuntu 12.04, MySQL 5.5, and php 5.5.7.
The result is as follows:
(1) the character set of the database table is utf8, and the set names utf8 is not used
Chinese characters can be inserted and read normally, but garbled characters are displayed in mysql.

(2) the character set of the database table is utf8, and set names utf8 is used.
Properly Insert and read Chinese characters. mysql displays the correct information.

(3) if the character set of the database table is not utf8, use set names utf8
Mysql shows that reading is a question mark.


Refer:
[1] go deep into Mysql character set settings. http://www.laruence.com/2008/01/05/12.html
[2] coding problem of PHP and MYSQL. http://blog.csdn.net/martinkro/article/details/5352474
[3] a deep understanding of the difference between set names and mysql (I) _ set_charset. http://www.laruence.com/2010/04/12/1396.html
[4] MySQL "set names xxx" character SET problem analysis-http://bbs.phpchina.com/thread-13861-1-1.html


Database table, mysql

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.