PHP PDO: Charset = UTF8: invalid chacheet phrase was specified on dsn line

I am connecting to MS SQL server with PDO using sqlsrv driver.

The PHP version is 5.3.24. A working connection is as follows:

$dsny = "sqlsrv:Server=xx1;Database=xx2"; $usery = 'xx3'; $passwordy = 'xx4'; $dbhy = new PDO($dsny, $usery, $passwordy); 

**

But I need to set the characters, and then I will try the following:

 $dsny = "sqlsrv:Server=xx1;Database=xx2;charset=utf8"; $usery = 'xx3'; $passwordy = 'xx4'; $dbhy = new PDO($dsny, $usery, $passwordy); 

When I add charset, I get this error: "Fatal error: exception thrown" PDFException "with the message" SQLSTATE [IMSSP]: invalid keyword "charset" was specified in dsn string "

So what can cause this error?

From what I read, I need to do this since I am launching a new version of PHP.

+4
source share
1 answer

You need to apply the attribute after connecting:

 $dbhy->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8); 

See: https://msdn.microsoft.com/en-us/library/ff628157(v=sql.105).aspx

+3
source

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


All Articles