How to solve the problem of garbled characters when php reads mysql database

一个新手
Release: 2023-03-15 22:26:01
Original
1398 people have browsed it

About the solution to garbled characters when PHP reads mysql database

When PHP reads mysql, the following places involve the character set.
1.Specify the character set of the database table when creating the database table. For example

create table tablename
(
id int not null auto_increment,
title varchar(20) not null,
primary key ('id')
)DEFAULT CHARSET =UTF8;
Copy after login

2. The character set of mysql
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.
Try to use mysqli::set_charset(mysqli:set_charset) instead of "SET NAMES"

  • $db = new mysqli('localhost','user','passwd','database_name');
    $db->set_charset('utf8');
    Copy after login

Copy code
Note that it is utf8, not utf-8
(There is a problem here that the database and php have unified encoding, but if mysqli is not called ::set_charset() function, garbled characters still appear 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; 
)
Copy after login

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

  • <meta http-equiv="content-type" content="text/html; charset=utf-8">
    Copy after login

    复制代码
    5. php文本文件所使用的字符集。
    在linux下可以用vim打开文件,输入
    :set encoding
    查看文件使用的字符集

    要保证不乱码,需要保证文件自身的编码,HTML里指定的编码,PHP告诉Mysql的编码(包括character_set_client和character_set_results)统一。同时使用mysqli:set_charset()函数或”SET NAMES”。

    针对“3”后面的问题,写了几个例子,测试链接数据库后,设置和不设置字符集时的结果。测试环境Ubuntu 12.04,MySQL 5.5,php 5.5.7。
    结果如下:
    (1) 数据库表字符集是utf8,不使用set names utf8
    能正常插入、读出中文,但是在mysql中显示乱码

    (2) 数据库表字符集是utf8,使用set names utf8
    能正常插入、读出中文,mysql中显示正确

    (3) 数据库表字符集不是utf8,使用set names utf8
    mysql中显示,读出都是问号。

    The above is the detailed content of How to solve the problem of garbled characters when php reads mysql database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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