【问题标题】:How to store special charcters as UTF-8 format in database如何将特殊字符以 UTF-8 格式存储在数据库中
【发布时间】:2016-02-02 09:44:51
【问题描述】:

我正在尝试将“£”存储在数据库中。我还在集合中将此字段标记为utf8_general_ci。但它保存为�。我还在元标记 HTML 文件中设置了 UTF-8。这里缺少什么

-----编辑-----------

我检查过,那个页面上有一个编辑器(富文本)。如果我使用这个编辑器保存数据,那么它会生成 - 但如果我使用文本框,那么它工作正常。我不知道这有什么问题以及如何调试这个问题

$query = "insert into field_master set fk_t_id = '".$_POST['t_id']."', fk_g_id = '".$_POST['g_id']."', fk_f_id = '".$_POST['f_id_'.$inc_i.'_'.$inc_j]."'".$str.", field_value = '".$field_value."', field_required = '".$required."', fk_section_id = '".$_POST['section_id_'.$inc_i]."', section_order = '".$_POST['section_order_'.$inc_i]."', field_order = '".$_POST['temp_order_'.$inc_i.'_'.$inc_j]."'";
$smt = $class->dbh->prepare($query);
$smt->execute();

【问题讨论】:

  • INSERT INTO table (column) VALUES ('£');在 phpmyadmin 中手动尝试此操作
  • 如果查询执行属性意味着,你需要修复你的代码..显示你的代码..你必须需要使用 ENT_QUOTE、'utf-8' 或 SET NAMES utf8 等。
  • @devpro: 我已经设置了 $stmt1 = $class->dbh->prepare("SET NAMES 'utf8'");
  • 手动运行插入查询

标签: php mysql iis utf-8 rich-text-editor


【解决方案1】:
The examples shown here assume use of the utf8 character set and utf8_general_ci collation.

Specify character settings per database. To create a database such that its tables will use a given default character set and collation for data storage, use a CREATE DATABASE statement like this:

CREATE DATABASE mydb
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;
Tables created in the database will use utf8 and utf8_general_ci by default for any character columns.

Applications that use the database should also configure their connection to the server each time they connect. This can be done by executing a SET NAMES 'utf8' statement after connecting. The statement can be used regardless of connection method: The mysql client, PHP scripts, and so forth.

In some cases, it may be possible to configure the connection to use the desired character set some other way. For example, for connections made using mysql, you can specify the --default-character-set=utf8 command-line option to achieve the same effect as SET NAMES 'utf8'.

If you change the default character set or collation for a database, stored routines that use the database defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly.

Specify character settings at server startup. To select a character set and collation at server startup, use the --character-set-server and --collation-server options. For example, to specify the options in an option file, include these lines:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.

I hope these information would be useful to you.

【讨论】:

    【解决方案2】:

    当您确实搞砸了 utf8 使用时,黑色菱形中的问号。简单的答案是通过说(在 html 中)<meta charset=ISO-8859-1>(别名 latin1)来进一步搞砸它。

    “正确”的答案是始终使用 utf8:

    • 客户端中的字符采用 utf8 编码。
    • 连接是 utf8 -- 通过在连接时执行SET NAMES utf8 或某些特定于语言的语法。 (见下文)
    • CHARACTER SET utf8 在列/表上。
    • HTML:<meta charset=UTF-8>

    不同的事情发生取决于你违反了哪一个。

    如果这些注释不足以解决您的问题,请执行SELECT col, HEX(col) FROM ... 以查看表格中的实际内容。对于£,您应该看到十六进制C2A3。如果你得到C382C2A3,你就有了“双重编码”的问题。如果您看到(不是十六进制)£,则您遇到了“Mojibake”问题。如果以上都不是,那么这就是使字符集问题成为挑战的原因。

    COLLATION(如utf8_general_ci)用于排序;它与本次讨论无关,只有 CHARACTER SET(utf8 或 utf8mb4)是相关的。

    mysqli接口:mysqli_set_charset('utf8');

    PDO接口:$db = new PDO('dblib:host=host;dbname=db;charset=UTF8', $user, $pwd);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      相关资源
      最近更新 更多