ColdFusion - 将阿拉伯语/波斯语字符插入 mysql
我使用以下代码将我的数据插入 mysql db:
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.
相关文章