php mysql 查询结果 utf8mb4_bin 编码为 utf-8 for html

php mysql query results utf8mb4_bin encoding to utf-8 for html

Mysql table 看起来像这样:

实际内容是这样的:

我试过这些:

在model.php

 if (mysqli_num_rows($result) > 0) {
        //iterate
        while($row = mysqli_fetch_assoc($result)){
            //get username
            $disease = mb_convert_encoding($row['disease'], "UTF-8", "auto");
            $food = mb_convert_encoding($row['food'], "UTF-8", "auto");
            $en_name = mb_convert_encoding($row['en_name'], "UTF-8", "auto");
            $health_effect = mb_convert_encoding($row['healthEffect'], "UTF-8", "auto");
            $metabollite = mb_convert_encoding($row['metabollite'], "UTF-8", "auto");
            //$citation = $row['citation'];
            $next_row = array("Disease"=>$disease, "Food"=>$food,
                "Name"=>$en_name, "Health Benefits"=>$health_effect, "Metabollite"=>$metabollite);
                //"Sources"=>$citation);
            $matches_arr[] = $next_row;
        }
    }
    $utfArr = array_map("utf8_encode", $matches_arr);//this does not work for associate arrays I think

回到 controller.php 我又试了一次

 //encode in UTF8
 foreach($matches_arr as $col => $val){
 $utf8_val = mb_convert_encoding($val, "UTF-8", "auto");
 $matches_arr[$col] = $utf8_val;
 }
//send it back to view
header('Content-Type: application/json');
$disease_json = json_encode($matches_arr, JSON_UNESCAPED_UNICODE);
echo $disease_json;

但它在日文中输出 table 列的问号:

我看到了这个: 但我认为我的情况不同,因为 mysql 数据库已经用 utf8mb4_bin 编码,而不是晦涩的日文编码。

无论如何,从那个答案我试着把这个标签放在 controller.php 和 model.php

的顶部
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />"

并检查我的 view.php 是否具有字符集:

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Jekyll v4.1.1">
    <title>Dashboard Bioactive Compounds</title>
    <!-- boostrap comes first -->
    <link rel="stylesheet" href="css/bootstrap.css">
    <meta charset="utf-8">

实际上它有两次。
当我检查查询结果变量时,我看到 model.php 中的查询结果已经只是 phpstorm 调试器控制台中的问号。
我怎样才能解决此问题以在视图页面上显示日语?

MySQL的CHARACTER SET utf8mb4相当于外界的UTF-8。因此不需要转换。

问号表示

  • 要存储的字节未编码为utf8/utf8mb4。解决这个问题。
  • 数据库中的列不是 CHARACTER SET utf8(或 utf8mb4)。解决这个问题。 (使用 SHOW CREATE TABLE。)
  • 此外,检查读取时的连接是否为 UTF-8。

link 还提供了一些调试信息。

请注意,问题可能出在 INSERT 端——这意味着数据丢失了。但是,显示似乎表明数据已正确存储。可能是多余的 mb_convert_encoding 调用导致了问题。