[英]Define encoding when using PDO ODBC SQL Server connection?
I connect to a SQL Server using like this:我使用如下方式连接到 SQL 服务器:
$pdo = new PDO("odbc:DRIVER=ODBC Driver 17 for SQL Server;SERVER=$serverName;DATABASE=$dbName", $username, $password);
The PDO
instance is created, and I can run SQL queries with it, but sometimes I get charset related errors. PDO
实例已创建,我可以使用它运行 SQL 查询,但有时我会遇到与字符集相关的错误。
The database collation is non unicode (latin2), and I would like to get the response in UTF8.数据库排序规则是非 unicode (latin2),我想得到 UTF8 的响应。 How is it possible to define it?怎么可能定义它?
I was able to find some parameters like PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES..."
but those are not available here, because I have an SQL Server.我能够找到一些参数,例如PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES..."
但这些参数在此处不可用,因为我有一个 SQL 服务器。
you have to try as below to add Client_CSet=UTF-8;您必须尝试如下添加 Client_CSet=UTF-8; to your connection code.到您的连接代码。
$pdo = new PDO("odbc:DRIVER=ODBC Driver 17 for SQL Server;SERVER=$serverName;DATABASE=$dbName;Client_CSet=UTF-8;Server_CSet=Windows-1251", $username, $password);
Only you need add charset=utf8
in dns
varible.只有您需要在dns
变量中添加charset=utf8
。 You can try this configuration:你可以试试这个配置:
$dns = "odbc:DRIVER=ODBC Driver 17 for SQL Server;SERVER=$serverName;DATABASE=$dbName;charset=utf8"
$pdo = new PDO($dns, $username, $password);
Similar question . 类似的问题。 Hope help you.希望能帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.