2016-02-02 67 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);

相關問題