PHP + SQL Server - 如何为连接设置字符集?

我正在尝试通过 php 在 SQL Server 数据库中存储一些数据.

I'm trying to store some data in a SQL Server database through php.

问题是特殊字符没有正确转换.我的应用程序的字符集是 iso-8859-1服务器使用的是windows-1252.

Problem is that special chars aren't converted properly. My app's charset is iso-8859-1 and the one used by the server is windows-1252.

在插入之前手动转换数据没有帮助,似乎有一些转换正在进行中.

Converting the data manually before inserting doesn't help, there seems to be some conversion going on.

运行 SQL 查询set char_convert off"也无济于事.

Running the SQL query 'set char_convert off' doesn't help either.

有人知道我怎样才能让它工作吗?

Anyone have any idea how I can get this to work?

我试过 ini_set('mssql.charset', 'windows-1252');也一样,但也没有结果.

I have tried ini_set('mssql.charset', 'windows-1252'); as well, but no result with that one either.

推荐答案

客户端字符集是必要的,但还不够:

Client charset is necessary but not sufficient:

ini_set('mssql.charset', 'UTF-8');

我搜索了两天如何通过 PHP 将 UTF-8 数据(来自网络表单)插入到 MSSQL 2008 中.我到处都读到你不能,你需要先转换到 UCS2(就像 cypher 的解决方案推荐的那样).在 Windows 上 SQLSRV 据说是一个很好的解决方案,我无法尝试,因为我是在 Mac OSX 上开发的.

I searched for two days how to insert UTF-8 data (from web forms) into MSSQL 2008 through PHP. I read everywhere that you can't, you need to convert to UCS2 first (like cypher's solution recommends). On Windows SQLSRV said to be a good solution, which I couldn't try, since I am developing on Mac OSX.

但是,FreeTDS 手册(PHP mssql 在 OSX 上使用的)说在开头引用前添加一个字母N":

However, FreeTDS manual (what PHP mssql uses on OSX) says to add a letter "N" before the opening quote:

mssql_query("INSERT INTO table (nvarcharField) VALUES (N'űáúőűá球最大的采购批发平台')", +xon);

根据这个讨论,N 字符告诉服务器转换为 Unicode.https://softwareengineering.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server

According to this discussion, N character tells the server to convert to Unicode. https://softwareengineering.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server

相关文章