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

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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.

回答1:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!