MsSQL Encoding Problem

I have a database in MsSQL with which I connect to my PHP code through PDO objects. there is a "Kadź" column in the database.

Now - if I do

SELECT Kadź FROM <tablename> 

from my SQL Server Management Studio - everything works fine, I get results. However, when I try to execute

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

I get

SQLSTATE [HY000]: General error: 207 General SQL Server error: check messages with SQL Server [207] (severity 16) [(null)]

error. Querying any other column does not cause any problems, but it does. I suspect this because of the "ź" character in the name of this column, which is incorrectly encoded on the route between my PHP code and the database.

The comparison used by my server is "Polish_Cl_Al". I tried to fix it by adding an attribute

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

but PDO answered me that the driver does not support it ...

in advance for any help

+6
source share
1 answer

You may 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); 
+5
source

Source: https://habr.com/ru/post/923456/


All Articles