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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度