Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsSQL encoding issue

i have a database in MsSQL, with which i connect from my PHP code through PDO objects. the database has column "Kadź".

Now - if i execute a

SELECT Kadź FROM <tablename>

query from my SQL Server Management Studio - everything works fine, i get the results. However, when i try to execute a

$sql = "SELECT Kadź FROM <tablename>";

i recieve a

SQLSTATE[HY000]: General error: 207 General SQL Server error: Check messages from the SQL Server [207] (severity 16) [(null)]

error. Querying any other column doesn't produce any problems, but this one does. I suspect it's because of that "ź" character in the name of this column, that is unproperly encoded on the route between my PHP code and the database.

The collation used by my server is "Polish_Cl_Al". i've tried fixing it by adding attribute

$this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAME'utf8'");

but the PDO answered me, that the driver doesn't support it...

thanks in advance for any help

like image 433
khartvin Avatar asked Aug 22 '12 07:08

khartvin


People also ask

Does SQL Server store UTF-8 encoding?

SQL Server does not store UTF-8 under any circumstances. You get either UTF-16 Little Endian (LE) via NVARCHAR (including NCHAR and NTEXT, but don't ever use NTEXT) and XML, or some 8-bit encoding, based on a Code Page, via VARCHAR (including CHAR and TEXT, but don't ever use TEXT ).

How do I encode Unicode characters in SQL Server?

2 Adding the UTF-8 option (_UTF8) enables you to encode Unicode data by using UTF-8. For more information, see the UTF-8 Support section in this article. SQL Server supports the following collation sets: Windows collations define rules for storing character data that's based on an associated Windows system locale.

Does SQL Server 2019 support UTF-8 collations?

Note that NCHAR and NVARCHAR remains unchanged and allows UCS-2/UTF-16 encoding. Like UTF-16, UTF-8 is only available to Windows collations that support Supplementary Characters, as introduced in SQL Server 2012. You can see all available UTF-8 collations by executing the following command in your SQL Server 2019 instance:

How do I use gb18030-encoded characters in SQL Server?

SQL Server provides support for GB18030-encoded characters by recognizing them when they enter the server from a client-side application and converting and storing them natively as Unicode characters. After they're stored in the server, they're treated as Unicode characters in any subsequent operations.


1 Answers

You can try:

$pdo = new PDO('dblib:host=localhost;dbname=databasename;charset=UTF-8', 'username', 'password');

or (for windows)

$smth->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
like image 166
Mihai Iorga Avatar answered Sep 29 '22 11:09

Mihai Iorga