Centos7下用php、mysqli读取MySQL中文出现问号?情况解决。

问题描述:mysql下的数据的的确确为中文数据,但是用mysqli读取显示成问号?

百度了很久,各种描述都没有正确得到解决,最终查看官方文档成功解决!

数据库内容:

Centos7下用php、mysqli读取MySQL中文出现问号?情况解决。_第1张图片

网页显示:

Centos7下用php、mysqli读取MySQL中文出现问号?情况解决。_第2张图片

查看手册说明

Use difference collation/character for connect, result.
You can set the collation before your query.

E.g. want to set the collation to utf8_general_ci
you can send the query "SET NAMES 'utf8'" first

query("SET NAMES 'utf8'");
$q=$mysqli->query("select * from test");
while($r=$q->fetch_assoc()) {
    print_r($r);
}
?>

There are many variables about character settings.
By running sql command, SHOW VARIABLES LIKE 'char%';
There are some variables control the character usage.

character_set_client
character_set_connection
character_set_database
character_set_results
character_set_server
character_set_system

Also SET NAMES can repalce with one or some settings like SET character_set_results='utf8';

重要的是设置字符格式(utf8加引号):$mysqli->query("SET NAMES 'utf8'");

结果

Centos7下用php、mysqli读取MySQL中文出现问号?情况解决。_第3张图片

官方手册:http://php.net/manual/en/mysqli.query.php

你可能感兴趣的:(PHP,MySQL)