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

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

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

数据库内容:

网页显示:

查看手册说明

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

<?php
$mysqli=new mysqli('localhost', 'root', 'password', 'test');
$mysqli->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'");

结果

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

猜你喜欢

转载自blog.csdn.net/weixin_40096730/article/details/88389803
今日推荐