问题描述
我使用以下代码将数据插入mysql数据库:
I insert my data into mysql db with this code:
<cfprocessingdirective pageEncoding="utf-8">
<cfset setEncoding("URL", "utf-8")>
<cfset setEncoding("Form", "utf-8")>
<cfcontent type="text/html; charset=utf-8">
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<cfparam name="postTextBox" default="" type="String">
<cfoutput>
<form action="index.cfm" method="POST" name="form">
<input name="postTextBox" type="text"/>
<input name="" type="submit" value="Submit" />
</form>
</cfoutput>
<cfquery name="myQuery" datasource="hello">
insert into ad (name)
values(N<cfqueryparam value=#postTextBox# cfsqltype="cf_sql_varchar">)
</cfquery>
</body>
</html>
问题是当我插入存储在数据库中的阿拉伯语或波斯语字符时,例如"?????"但是英文字符没有问题.我使用ColdFusion 10和mysql.
The problem is when I insert arabic or persian characters that's store in database something like "?????" but there is no problem with english characters.I use ColdFusion 10 and mysql.
致谢
推荐答案
(来自评论...)
检查列或表的字符集.确保它支持unicode字符.例如,UTF-8:
Check the charset of your column or table. Make sure it supports unicode characters. For example, UTF-8:
CREATE TABLE ( name varchar(500) CHARSET UTF8, ....)
此外,不要使用 N'literal'
语法,您也可以使用新的cfsqltype cf_sql_nvarchar
.经过这些更改,它应该可以正常工作.
Also, instead of using N'literal'
syntax, you may as well use the new cfsqltype cf_sql_nvarchar
. With those changes, it should work fine.
INSERT INTO ad ( name )
VALUES
(
<!--- always scope variables --->
<cfqueryparam value="#FORM.postTextBox#" cfsqltype="cf_sql_nvarchar">
)
旁注-与您的问题无关,但cfprocessingdirective
在这里无效.当您需要在CF脚本中嵌入或硬编码Unicode字符时使用它.因为您没有这样做,所以您不需要它.
Side note - Nothing to do with your question, but cfprocessingdirective
has no effect here. It is used when you need to embed, or hard code, Unicode characters within a CF script. Since you are not doing that, you do not need it.
这篇关于ColdFusion-将阿拉伯/波斯字符插入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!