将latin1_swedish_ci转换为utf8

时间:2015-08-02 21:50:16

标签: php mysql pdo utf-8

我从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

我似乎仍然得到同样的错误?

1 个答案:

答案 0 :(得分:1)

不要使用任何转换功能 - 在整个处理过程中指定utf8。

确保数据编码为utf8。

使用PDO时:

$db = new PDO('dblib:host=host;dbname=db;charset=UTF-8', $user, $pwd);

表定义(或至少列):

CHARACTER SET utf8

网页输出:

<meta ... charset=UTF-8>