ColdFusion - Inserting arabic/persian characters to mysql

↘锁芯ラ 提交于 2019-12-02 03:18:53

(From comments ...)

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, ....)

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">
    )

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.

The issue of having "????" marks instead of Arabic Characters could be two issues; With the assumption that you are using MySQL 5, with ColdFusion 10 or 11:

First Issue: It could be that you have not setup the MySQL database to UTF-8, in order to convert all your DB to UTF-8, use the following commands on MySQL:

Use the ALTER DATABASE and ALTER TABLE commands.

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Second Issue: The ColdFusion DataSource has not been setup properly and it will need two more additional strings to be added using the ColdFusion Data Source options.

Go to the ColdFusion Administrator, and open the Data Source page and click on the data Source that you have setup already.

Data & Services > Datasources > MySQL 5

Click the Data Source Name and click Show Advanced Settings, on the Connection String feild add the following two sting values.

useUnicode=true&characterEncoding=utf8

Once after you have saved the configuration details, the ColdFusion will start converting the data in to UTF-8 charachter set, and you will have proper Arabic or Persian characters.

Note: If you are hosting with some hosting service providers, such as my hosting company, you will have to request this to be added to the Data-source for your DB from the hosting support teams, I'm sure most of the hosting companies will have no issue in adding them to you. Also note they might have a load-balancing environment, and in this case you might some times get the '????' and some times the proper 'Arabic' characters, in this case make sure they add the above string to all their CF servers on the H/A environment (i.e. this happened to me).

Hope this helps resolve the issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!