2016-02-02 35 views
0

我试图在数据库中存储“£”。我也在collection中将这个字段标记为utf8_general_ci,但它保存为 。我还在元标记HTML文件中设置了UTF-8。这到底是怎么失踪如何将特殊字符存储为数据库中的UTF-8格式

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

我检查了一下,有一个编辑器( 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

INSERT INTO表(列)VALUES( '£');试试这个manualy在phpmyadmin – devpro

+1

如果查询执行属性,它意味着,你需要修复你的代码..显示你的代码..你必须使用ENT_QUOTE,'utf-8'或SET NAMES utf8等。 – devpro

+0

@devpro:I已经设置了$ stmt1 = $ class-> dbh-> prepare(“SET NAMES'utf8'”); – kreya

回答

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. 
0

问号的黑色钻石,当你真的搞砸了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);

相关问题