dongtanghuan1885 2012-04-17 00:50
浏览 60
已采纳

PHP将utf8Æ(195 134)转换为198

I have a strange problem in PHP, hopefully someone has seen this before.

I am storing the Æ character (AE) in a MySql database that is set to utf8. I have a PHP application that displays the character in a browser correctly on my local machine (Windows 7), but when the same code and database are used in production (GoDaddy Linux Hosting), the character is being converted to a single ASCII character (198), which displays as the dreaded question mark in a black triangle. I have confirmed that the database and code are exactly the same in both places. I can iterate over the bytes as an array and see that what is coming out of MySql is two bytes, \195 \134, but what PHP sends has been converted somehow in the production environment to a single 198.

I have ruled out browser issues with character encoding. I get the same behavior in all browsers using utf8 encoding. I can look at the raw response and see the issue.

  • 写回答

2条回答 默认 最新

  • duankeng1911 2012-04-17 01:24
    关注

    I had a suspicion that the PDO connection was the culprit, but I thought I was covered by my initialization code. Turns out, it doesn't work on all platforms. In addition to adding "set names 'utf8'" as a PDO attribute, you also (or instead) need to actually execute a query with the command before using the PDO reference.

    // This doesn't seem to work on all platforms
    $pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
    
    // This seems like a hack, but it works on GoDaddy Linux Hosting
    $sth = $pdo->prepare("set names 'utf8'");
    $sth->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)