PHP + SQL Server - 如何设置字符集连接? [英] PHP + SQL Server - How to set charset for connection?

查看:949
本文介绍了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');

我搜索了两天如何将UTF-8数据(从Web表单)插入到MSSQL 2008中PHP。我读到无处不在,你需要先转换到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。
http://programmers.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. http://programmers.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server

这篇关于PHP + SQL Server - 如何设置字符集连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆