utf8_unicode_ci 的 JSON 数据的 PHP 标头中的内容类型字符集 - Ajax 调用

Posted

技术标签:

【中文标题】utf8_unicode_ci 的 JSON 数据的 PHP 标头中的内容类型字符集 - Ajax 调用【英文标题】:Content Type Character Set in PHP Header of JSON Data for utf8_unicode_ci - Ajax Call 【发布时间】:2016-07-05 23:22:34 【问题描述】:

我使用 utf8_unicode_ci 作为 mysql 表中的一列来存储泰米尔语字符。

我正在实现一个 AngularJS 项目,因为我正在调用 php 服务,返回类型是 JSON 数据。我无法获得实际的字符,而不是我得到 ?????????

我的 PHP 示例源代码:

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$outp = "";

$sql_select = "";
$sql_select .= "SELECT * FROM poll_quest WHERE qid = $nqid LIMIT 1 ";
$bQuery = mysql_query($sql_select, $link);
while ($bRow = mysql_fetch_array($bQuery)) 

    $qflag = true;

    $outp .= ' "ID": ' . $bRow['qid'] . ',';
    $outp .= '"Ans":"' . $bRow['ans_tam'] . '" ';


$outp ='"records":['.$outp.']';
echo($outp);

?>

我的泰米尔语是

மோசம் மோசமாக இல்லை நன்று மிகநன்று

MySQL 表结构快照:

MySQL 表数据快照:

输出 JSON 数据快照:


   "records":[
      
         "ID":"1",
         "Ans":"??. ????????"
      ,
      
         "ID":"2",
         "Ans":"??. ?????????"
      ,
      ........
      
         "ID":"5",
         "Ans":"??. ??????????"
      
   ]

请帮助我,如何获取响应 JSON 中的实际字符...

【问题讨论】:

如果你只是打印你得到的东西?我的意思是...使用带有 header("Content-Type: text/html; charset=utf-8"); 的 echo @Boctulus - 参考输出 JSON 数据快照,我在浏览器中得到了这个。 连接到数据库后尝试使用 mysqli_set_charset($conn,'utf8') 【参考方案1】:

首先,MySQL 驱动程序,即 mysql_* 已被弃用,有利于 PDO 或 MySQLi。话虽如此,让我们继续前进。

您需要在连接上设置字符集的答案,即您需要执行以下操作才能使其正常工作 -

<?php
...
$link = mysqli_connect(...);
mysql_set_charset('utf8', $link); // This is your answer
...

为了将来的参考,我已经修改了上面的代码来使用 MySQLi 来帮助你一点 -

<?php
// NOTE : I prefer using PDO but I have used mysqli for this example
// NOTE : The following code HAS NOT BEEN TESTED!

header('Content-Type: application/json; charset=UTF-8');

// Connection information
$link = new mysqli($host, $user, $password, $dbname);

$link->set_charset('utf8'); // This is important or mysql_set_charset('utf8', $link) if you insist on using mysql_*

// Check connection
if (mysqli_connect_errno()) 
    printf('Connect failed: %s\n', mysqli_connect_error());

    exit();


$outp = '';

// Always use prepared statements!!
if ($stmt = $link->prepare('SELECT qid, ans_tam FROM poll_quest WHERE qid = ? LIMIT 1')) 
    $stmt->bind_param('i', $nqid); // user 's' if this is a string for the first parameter

    $stmt->execute();

    $stmt->bind_result($qid, $ans_tam);

    while ($stmt->fetch()) 
        $qflag = true;

        $outp .= ' "ID" : ' . $qid . ', "Ans" : "' . $ans_tam . '" ';
    

    $stmt->close();


echo('"records":[' . $outp . ']');

【讨论】:

【参考方案2】:

我相信你唯一想念的是

mysql_set_charset('utf8');

即使您为表或字段设置了utf8_unicode_ci,与数据库的连接也不一定会自动设置为使用utf8 字符编码。


注意:您应该考虑使用utf8mb4_unicode_ci instead of utf_unicode_ci,并且您应该真正考虑在数据库(至少表)级别而不是字段级别上设置字符集和排序规则(我看到您将latin1_swedish_ci 作为默认值,表示您的整个数据库是latin1_swedish_ci

【讨论】:

以上是关于utf8_unicode_ci 的 JSON 数据的 PHP 标头中的内容类型字符集 - Ajax 调用的主要内容,如果未能解决你的问题,请参考以下文章

搜索输入 GET/POST 数据无法理解来自 MySQL DB 的 utf8_unicode_ci 泰语数据

mysql 5.5 utf-8 排序规则 utf8_unicode_ci, pymysql

MySQL 中的 utf8_general_ci 和 utf8_unicode_ci 有啥区别?

将 utf8_general_ci 表和字段转换为 utf8_unicode_ci

如何将所有行的排序规则从 latin1_swedish_ci 更改为 utf8_unicode_ci?

“utf8_unicode_ci”和“utf8_unicode_520_ci”有啥区别