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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!