Home > Backend Development > PHP Tutorial > Summary of garbled code problems when reading mysql in php

Summary of garbled code problems when reading mysql in php

WBOY
Release: 2016-07-25 09:13:29
Original
852 people have browsed it

php reads mysql’s summary of the garbled problem, friends in need can refer to it.


When PHP reads mysql, the following places involve character sets.

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;
Copy code
2. Mysql character set
There are three important variables in mysql, character_set_client, character_set_results, and character_set_connection.
By setting character_set_client, tell Mysql what encoding method PHP stores in the database.
By setting character_set_results, tell Mysql what kind of encoded data PHP needs to get.
By setting character_set_connection, tell Mysql what encoding to use for the text in the PHP query.

3. After connecting to the database, set the default character encoding used when transmitting characters between databases.
Use mysqli::set_charset() or mysqli::query('set names utf8') to set it.
Try to use mysqli::set_charset(mysqli:set_charset) instead of "SET NAMES"PS: (refer to this article)
  1. $db = new mysqli('localhost' ,'user','passwd','database_name');
  2. $db->set_charset('utf8');
Copy the code
Note that it is utf8, not utf-8
(A problem here is that the database and PHP have unified the encoding, but if the mysqli::set_charset() function is not called, garbled characters will still appear when reading the data. Why is this?)
(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. The character set used by the html page. Set

Copy code
in meta tag5. The character set used by php text files.
Under Linux, you can use vim to open the file and enter
:set encoding
Check the character set used by the file

To ensure that there is no garbled code, you need to ensure that the encoding of the file itself, the encoding specified in HTML, and the encoding that PHP tells Mysql (including character_set_client and character_set_results) are unified. Also use the mysqli:set_charset() function or "SET NAMES".


In response to the question after "3", I wrote several examples to test the results of setting and not setting the character set after linking to the database. Test environment Ubuntu 12.04, MySQL 5.5, php 5.5.7.
The results are as follows:
(1) The database table character set is utf8, do not use set names utf8
Can insert and read Chinese normally, but garbled characters are displayed in mysql

(2) The database table character set is utf8, use set names utf8
Chinese can be inserted and read normally, and it is displayed correctly in mysql

(3) The database table character set is not utf8, use set names utf8
It is displayed in mysql and read out as question marks.


Reference:
[1] In-depth Mysql character set settings. http://www.laruence.com/2008/01/05/12.html
[2] Encoding issues with PHP and MYSQL. http://blog.csdn.net/martinkro/article/details/5352474
[3] In-depth understanding of the difference between SET NAMES and mysql(i)_set_charset.http://www.laruence.com/2010/04/12/1396.html
[4] Analysis of MySQL’s “SET NAMES xxx” character set problem. http://bbs.phpchina.com/thread-13861-1-1.html


Database table, mysql


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