S
S
SlavaMorg2020-05-30 00:06:37
Character encoding
SlavaMorg, 2020-05-30 00:06:37

How to overcome bugs when connecting to MsSql?

Gave access to an external database on mssql. Well, I don't know him. I do not understand how to explicitly specify the encoding there.
Russian strings come in the form "??? ?????". It's the questions. The database is not mine, I can't change it somehow. I can only read. Google didn't help. As I understand it, there is no set names in mssql. Where to prescribe collate did not figure it out. It seems to be needed for sorting, but I would just read a row in the table.
Made a request to INFORMATION_SCHEMA.COLUMNS. Returned the following parameters about this field:
'DATA_TYPE' => 'nvarchar'
'CHARACTER_SET_NAME' => string 'UNICODE'
'COLLATION_NAME' => string 'Latin1_General_CI_AI'

Working on php. I set up the connection like this:

$dbh = new PDO("dblib:host=hostname:port;dbname=db", "login", "pass");

Tried to add charset to the first parameter - does not help. Inserted utf8 and a variety of "styles" Latin1_General_CI_AI.
Well, the sql query is the most primitive:
SELECT TOP 100 * FROM Discounts;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2020-05-30
@d-stream

https://docs.microsoft.com/en-us/sql/connect/php/h...

B
Barmunk, 2020-05-30
@Barmunk

dblib itself does not work well with Cyrillic, you need to constantly run the results through iconv, look for the desired locale, etc. Therefore, freeTDS is installed on the server, which can convert data on the fly.
The config specifies the connection:

[SERV_UTF8]
    host = serv.site.ru
    port = 1433
    tds version = 8.0
    client charset = UTF-8

and in php
$dbh = new PDO("dblib:host=SERV_UTF8;dbname=db",  $username, $pw);

or use the SQLSRV driver, which no longer has these problems. It can be found on the official Microsoft website.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question