php - 简体中文字符显示为问号菱形

标签 php html mysql mysqli character-encoding

我已经设置了元并检查 mysql 似乎已经在 utf 8 上,这里似乎有什么问题?

HTML

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

enter image description here

PHP 数据库连接

$DB_NAME = 'ssl';
$DB_HOST = 'localhost';
$DB_USER = 'dbuser';
$DB_PASS = 'dbpass';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);


// $mysqli->character_set_name();
mysqli_set_charset($mysqli,"utf8");


if (mysqli_connect_errno())
{
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

查询输出

$query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

显示

<h1><?php echo translate("In-Kind Donation"); ?></h1>

也尝试设置 header('Content-Type: text/html; charset=UTF-8') 但结果仍然相同

enter image description here

编辑:添加翻译功能信息

 function translate($phrase) {

    if(!isset($_SESSION)) 
    { 
        session_start(); 
    }

    require 'db/mysql.php';

        $_GET['lang'] = $_SESSION['lang'];
        $lang_session = $_GET["lang"];

        if($lang_session == 'CN') {
            $translate_from = 'EN';
            $translate_to = 'CN';
        } 
        else {
            $translate_from = 'CN';
            $translate_to = "EN";
        } 

        $query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
        $row = mysqli_fetch_array($query);

        if(isset($row['lang']) && $row['lang'] !== ""){

            return $row['lang'];
        }
        else{
           return $phrase;
        }
    }

最佳答案

有些汉字需要4个字节来编码。 MySQL 的 CHARACTER SET uf8停在 3 字节编码处。更改为utf8mb4

mysqli_set_charset($mysqli,"utf8");
mysqli_set_charset($mysqli,"utf8mb4");

您的<meta ... charset=UTF-8" />标签所写内容正确。

(这也适用于表情符号。)

关于php - 简体中文字符显示为问号菱形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465981/

相关文章:

php - 从 Guzzle 中捕获 ConnectException

php - 分页 - 检查是否存在下一个/上一个记录

php - 如何在 PHP 中仅显示选定的行?

php - 如何将查询结果显示为4列并移动到4列之后的另一行?

php - 用于工作日/周末计数的 MySQL/PHP 算法(每月)

php - Firefox 弹出窗口不会接收 cookie

html - 设置浏览器缩小时的水平滚动条

mysql - Rails 部署期间的 "error 28 from storage engine"

mysql - sql 两张表的组合和distinct只有特定的值

php - 在没有 Cron Jobs 的情况下运行 PHP 文件?