【问题标题】:convert latin1_swedish_ci to utf8将 latin1_swedish_ci 转换为 utf8
【发布时间】: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

标签: php mysql pdo utf-8


【解决方案1】:

不要使用任何转换函数——在整个处理过程中指定 utf8。

确保数据采用 utf8 编码。

使用 PDO 时:

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

表定义(或至少列):

CHARACTER SET utf8

网页输出:

<meta ... charset=UTF-8>

【讨论】:

    猜你喜欢
    • 2011-10-02
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多