【问题标题】:Error inserting scandic letters into database将扫描字母插入数据库时​​出错
【发布时间】:2015-12-14 15:04:38
【问题描述】:

我正在尝试在我的数据库中插入包含扫描字母的网址,例如:

ÄÖäöÅå

我正在使用:

  • Opensuse 13.2 64 位 Linux 和 MariaDB。
  • MySQL 服务器版本:5.5.44-MariaDB openSUSE 包
  • PHP 版本为 5.4.20

当我尝试插入时,我收到以下错误消息:

不正确的字符串值:'\xC4HK\xD6.

此查询确认字符集和排序规则设置正确:

if (mysql_query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci")) {
    echo "Character set OK !";
}

我的 MySQL 查询适用于除包含扫描字母的 URL 之外的所有内容:

if (mysql_query("INSERT INTO `table` (`address`) VALUES ('$URL')")){
    $insertCount++;
    echo "<br> insertcount = ".$insertCount."<br>";
} else {
    echo "MySQLerror = ".mysql_error()."<br>"; // Show MySQLerror

这是来自 MariaDB 的 MySQL 信息,显示所有内容都设置为 utf8mb4

MariaDB [(none)]> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

+--------------------------+--------------------+
| Variable_name            | Value              |
+--------------------------+--------------------+
| character_set_client     | utf8mb4            |
| character_set_connection | utf8mb4            |
| character_set_database   | utf8mb4            |
| character_set_filesystem | binary             |
| character_set_results    | utf8mb4            |
| character_set_server     | utf8mb4            |
| character_set_system     | utf8               |
| collation_connection     | utf8mb4_unicode_ci |
| collation_database       | utf8mb4_unicode_ci |
| collation_server         | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0,00 sec)

我怎样才能正确插入扫描字母?


编辑

@Monty:这些是我的数据库设置:

MariaDB [(none)]> show variables like '%colla%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_unicode_ci |
| collation_database   | utf8mb4_unicode_ci |
| collation_server     | utf8mb4_unicode_ci |
+----------------------+--------------------+
3 rows in set (0,00 sec)

MariaDB [(none)]> show variables like '%charac%';
+--------------------------+------------------------------+
| Variable_name            | Value                        |
+--------------------------+------------------------------+
| character_set_client     | utf8mb4                      |
| character_set_connection | utf8mb4                      |
| character_set_database   | utf8mb4                      |
| character_set_filesystem | binary                       |
| character_set_results    | utf8mb4                      |
| character_set_server     | utf8mb4                      |
| character_set_system     | utf8                         |
| character_sets_dir       | /usr/share/mariadb/charsets/ |
+--------------------------+------------------------------+
8 rows in set (0,00 sec)

MariaDB [(none)]> 

编辑

@Rick James:这是我回来的:

MariaDB [db]> SHOW CREATE TABLE table; +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | table | CREATE TABLE table ( addr varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (addr), UNIQUE KEY addr (addr) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='List' | +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0,00 sec)

MariaDB [数据库]>

【问题讨论】:

标签: php mysql sql-insert collation


【解决方案1】:

C4 和 D6 是 Ä 和 Ö 的 latin1 十六进制。

请通过SHOW CREATE TABLE 查看为相关列设置的CHARACTER SET。我怀疑是错误的latin1

而且,是的,您必须从mysql_* 界面切换。

【讨论】:

  • 看起来该列没有任何字符集,只有排序规则。请看我的编辑。这种行为有可能是由它引起的吗?
  • CHARACTER SET是从表中继承而来的,即utf8mb4。现在来抓挠我的头——你说得对,但还是有问题。
  • mysql_* 不支持字符集; mysqli_* 可以。 Reference.
【解决方案2】:

试试这个

验证存储数据的表是否有utf8字符集:

SELECT
  `tables`.`TABLE_NAME`,
  `collations`.`character_set_name`
FROM
  `information_schema`.`TABLES` AS `tables`,
  `information_schema`.`COLLATION_CHARACTER_SET_APPLICABILITY` AS `collations`
WHERE
  `tables`.`table_schema` = DATABASE()
  AND `collations`.`collation_name` = `tables`.`table_collation`
;

检查您的数据库设置:

show variables like '%colla%';
show variables like '%charac%';

将 utf-8 改为 utf8_general_ci

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

【讨论】:

  • 非常感谢您的回答,伙计,但我在将所有内容转换为 utf8mb4 之前使用了 utf8,但它也不起作用
  • 我试图在 mysql 版本 5.0.11-dev 中保存“ÄÖäöÅå”。它的插入。如果您使用的是 PHP,那么您可以使用 htmlspecialchars();就在插入数据库之前。在渲染之前使用 html_entity_decode();或者你可以使用 mysql_real_escape_string()
  • 好的,非常感谢,伙计,我会试试的,我会告诉你进展如何
  • 我试过 $URL = htmlspecialchars($URL);就在插入之前,当包含扫描字母时,它确实将 $URL 呈现为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 2019-10-11
  • 1970-01-01
  • 2013-04-25
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多