doushu8260 2015-12-11 19:44
浏览 43

php和pdo utf-8通过api的奇怪行为

I've met and interesting behavior with PDO which relates to UTF-8 encoding issues.

When inserting data I need to declare SET NAMES UTF-8 for data to be stored correctly. Ok so this is fine. BUT!

When selecting data (and fetching results) I specifically can't SET NAMES UTF-8, or otherwise the characters gets scrambled. Or if I use set names UTF-8 for selects I need to utf8decode the result set (as in translate to iso88591) if I want to see them correctly. This would suggest that my page would be interpreted as iso88591. However if I reduce my page to single page app (its an angularjs/PHP slim/pdo setup) the same functionality remains - UTF-8 names for pdo init for $http.post and explicitly no set names UTF-8 for select / $http.get. Also if I die and expose the data after fetching the results from UTF-8 collated table I need decode them to see correct data.

I made this hack in my db adapter construct to get around it but I rather would like to solve the whole issue since it has made me curious:

$init = array();
if ($write) {
    $init = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
}
$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS, $init);

Some facts:

  • all associated files are UTF-8.
  • all (search, view, insert) pages are correctly as "text/HTML;charset=UTF-8" (and of course there is the fact that I reduced all functionalities to one page and still could replicate this scenario)
  • headers from debugger translated as UTF-8.
  • PHP detects strings (insert and search) as UTF-8 when echoed in controller from request parameters BEFORE sql insert/select.
  • PHP ini default charset is UTF-8.
  • MySQL database and table charset UTF-8 and collation UTF-8 general.
  • editor is atom.
  • environment ubuntu 14.

Has anyone else encountered anything similar behavior? Could this have to do something with the headers sent with angular (XHR) as they are default? Though after the request and params are interpreted correctly as UTF-8 by server side so it would seem far fetched. There is a chance that this might have a mystical link to environment since its not a fresh vm but a local dev machine that has its own tweaks on it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码