【发布时间】:2015-08-02 21:50:16
【问题描述】:
我正在从 xml 导入数据,他们似乎使用“latin1_swedish_ci”,这导致我在使用 php 和 mysql (PDO) 时遇到很多问题。
我遇到了很多这样的错误:
General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
我想知道如何将它们转换为正确的 UTF-8 以存储在我的数据库中。
我试过这样做:
$game['human_name'] = iconv(mb_detect_encoding($game['human_name'], mb_detect_order(), true), "UTF-8", $game['human_name']);
我在这里找到的:PHP: Convert any string to UTF-8 without knowing the original character set, or at least try
但我似乎仍然遇到同样的错误?
【问题讨论】:
-
您要插入的表及其列的排序规则是什么?
-
在“utf8_general_ci”下
-
您是否尝试过强制您的 PDO 实例使用 UTF-8? db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
-
我还建议为您的编码功能尝试使用 'mb_convert_encoding' 而不是 'iconv' php.net/manual/en/function.mb-convert-encoding.php