Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP + SQL Server - How to set charset for connection?

Tags:

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

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.

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

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

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

like image 672
Maurice Avatar asked Aug 24 '09 13:08

Maurice


1 Answers

Client charset is necessary but not sufficient:

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

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.

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

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

like image 51
Mano Kovacs Avatar answered Nov 08 '22 21:11

Mano Kovacs