问题描述
我正在使用sqlsrv驱动程序通过PDO连接到MS SQL服务器.
I am connecting to an MS SQL server with PDO using the sqlsrv driver.
PHP版本是5.3.24.工作连接如下所示:
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);
当我添加字符集时,出现此错误:致命错误:消息为'SQLSTATE [IMSSP]的未捕获异常'PDFException':在dsn字符串中指定了无效的关键字'charset'""
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 ?
根据我的阅读,由于我正在运行新的PHP版本,因此我需要这样做.
From what i read i need to do like this since i am running a new PHP version.
推荐答案
您需要在连接后应用属性:
You need to apply the attribute after connecting:
$dbhy->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
请参阅: https://msdn.microsoft .com/en-us/library/ff628157(v = sql.105).aspx
这篇关于PHP PDO:Charset = UTF8:在dsn字符串中指定了无效的关键字charset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!