我想在数据库中存储“£”。我还在集合中将此字段标记为utf8_general_ci。但它保存为 。我还在元标记HTML文件中设置了UTF-8。这里缺少什么
----------------- EDIT -----------
我检查过,该页面上有一个编辑器(Rich Text)。如果我使用这个编辑器来保存数据,那么它会生成 但是如果我使用文本框那么它可以正常工作。我不知道这有什么问题以及如何调试这个问题
$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();
答案 0 :(得分:0)
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.
答案 1 :(得分:0)
当你真的搞砸utf8使用时,黑色钻石中的问号。简单的回答是通过说(在html中)<meta charset=ISO-8859-1>
(别名latin1)来进一步搞砸它。
'正确'的答案是在整个过程中使用utf8:
SET NAMES utf8
或某些特定于语言的语法。 (见下文)CHARACTER SET utf8
。<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);