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

确保数据编码为utf8。

使用PDO时:

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


表定义(或至少列):

CHARACTER SET utf8


网页输出:

<meta ... charset=UTF-8>

关于php - 将latin1_swedish_ci转换为utf8,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31776988/

10-15 22:24