Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO : Charset=UTF8 : An invalid keyword charset was specified in the dsn string

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

PHP version is 5.3.24. The working connection looks like this:

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

**

But i need to set characters, and then i try this:

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

When i add the charset i get this error: "Fatal error: Uncaught exception 'PDFException' with message 'SQLSTATE[IMSSP]: An invalid keyword 'charset' was specified in the dsn string'"

So what could be causing this fault ?

From what i read i need to do like this since i am running a new PHP version.

like image 558
Niels Avatar asked May 28 '13 20:05

Niels


1 Answers

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

like image 104
Kristian Williams Avatar answered Sep 28 '22 01:09

Kristian Williams