0

关于检索 UTF8 字符集中的字符,我对 drupal 6 有一个非常奇怪的问题。有趣的是,似乎只有 drupal API 在正确检索数据方面存在问题;当我直接使用 mysqli 以及从命令行获取数据时,一切似乎都很好。

数据库和表设置为正确的字符集和排序规则,如下所示:

    mysql> show variables like "collation_%";
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

mysql> show variables like "char_%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

当我从命令行检索数据时,一切似乎都正常。

   mysql> select id, name from web_gfo_article_type;
   | 294 | ДОСЛОВНО                                                      |
   | 295 | EXAMEN                                                        |
   | 296 | REVISIÓN                                                      |

在我的一个自定义 drupal 模块中,我使用 drupal API (db_query) 并使用直接方法使用 mysqli 检索相同的数据。

直接方法:

$mysqli = new mysqli("localhost", "my_db_user_name", "my_db_password", "my_db_name");
$r = $mysqli->query("select id, name from web_gfo_article_type");

while ($row = $r->fetch_object()) {
    print('<br/>' . $row->id . ' ' . $row->name);
}

直接接近的结果:

294 ДОСЛОВНО
295 EXAMEN
296 REVISIÓN

使用 drupal 数据库 API:

$r = db_query("select id, name from web_gfo_article_type");

while ($row = db_fetch_object($r)) {
    print('<br/>' . $row->id . ' ' . $row->name);
}

drupal db API 调用的结果:

294 ДОСЛОВÐО
295 EXAMEN
296 REVISIÓN

如您所见,drupal db API 检索数据的方式存在严重错误,这导致 UTF 数据在呈现时被咀嚼。以上所有结果都显示在已正确设置元标题信息的 HTML 页面中,即

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

有人可以为我指出可能发生的事情和做错的事情的正确方向。非常感激。

4

1 回答 1

0

只需确保您编写查询的 .module-file(?) 使用的是 utf-8?

于 2013-02-18T18:26:30.743 回答