dongliang1941 2017-04-18 12:59
浏览 58
已采纳

utf8_unicode_ci似乎不是UTF8

I got something strange with my mySql database... My tables are encode in "utf8_unicode_ci", but when I make a SELECT on it, data seems not to be UTF8 because json_encode give an empty string.

I have to re browse the result to re-encode the data in UTF8...

    $q = $this->db->prepare("SELECT ...");

    $q->execute();

    $data = $q->fetchAll();

    foreach ($data as $key => $value) {
        $data[$key] = utf8_encode($value);
    }

    return $data;

After that, json_encode do his job. But I don't understand why the SELECT result from my table encode in "utf8_unicode_ci" doesn't extract the data in UTF8 directly... It is very redundant to re-browse the array...

Thanks for help

  • 写回答

1条回答 默认 最新

  • duanchi4184 2017-04-18 13:13
    关注

    Having the Mysql tables encoded in UTF-8 is not enough. That only ensures that the data is "stored" in UTF-8. When it is "passed" to external sources, even the passing pipe has to be UTF-8 encoded. You will need to set char set for the connection link identifier as well.

    It works something like so:

    $mysqli = new mysqli("server", "user", "password", "test");
    $mysqli->set_charset("utf8");
    

    Also check out SET NAMES utf8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办