简体   繁体   English

PHP / PDO和SQL Server连接以及i18n问题

[英]PHP/PDO and SQL Server connection and i18n issues

In our web-app we use PHP5.2.6 + PDO to connect to a SQL Server 2005 database and store Russian texts. 在我们的网络应用程序中,我们使用PHP5.2.6 + PDO连接到SQL Server 2005数据库并存储俄语文本。

Database collation is Cyrillic_General_CI_AS , table collation is Cyrillic_General_CI_AS , column type is NVARCHAR(MAX) . 数据库排序规则是Cyrillic_General_CI_AS ,表排序规则是Cyrillic_General_CI_AS ,列类型是NVARCHAR(MAX)

We tried connecting to a database using two following schemes, both causing different problems. 我们尝试使用以下两种方案连接到数据库,两者都会导致不同的问题。

  1. PDO mssql: PDO mssql:

     $dbh = new PDO ('mssql:host='.$mssql_server.';dbname='.$mssql_db, $mssql_login, $mssql_pwd); 

    in which case a result of a simple query like that: 在这种情况下,这样的简单查询的结果:

     SELECT field1 FROM tbl1 WHERE id=1 

    shows field1 data truncated to 255 bytes. 显示截断为255个字节的field1数据。

  2. PDO odbc: PDO odbc:

     $dbh = new PDO ('odbc:DSN=myDSN;UID='.$mssql_login.';PWD='.$mssql_pwd); 

    in which case a result of the same query shows full not truncated data but with question marks instead of Russian symbols. 在这种情况下,同一查询的结果显示完整的非截断数据,但带有问号而不是俄语符号。


Notes: 笔记:

  • In the SQL Management Studio data is not truncated and Russian symbols are displayed properly as well. 在SQL Management Studio中,数据不会被截断,俄语符号也会正确显示。
  • We have Windows 2003 Enterprise Edition SP2 我们有Windows 2003 Enterprise Edition SP2

So what should we choose as a connection method and how to fix corresponding issues? 那么我们应该选择什么作为连接方法以及如何解决相应的问题呢?

Try executing SET NAMES "charset" after you connect. 连接后尝试执行SET NAMES "charset"

I don't know what the charset to match Cyrillic_General_CI_AS is, but try "Cyrillic"? 我不知道匹配Cyrillic_General_CI_AS的字符集是什么,但尝试“西里尔字母”?

我必须这样做才能从MSSQL中的NVARCHAR字段获取可用数据:

$_ = iconv('Windows-1252', 'UTF-8', $_);

I've always had the best luck using utf8_general_ci across the board - for connections, collations - everything. 我总是使用utf8_general_ci获得最好的运气 - 用于连接,整理 - 一切。

However, I only have that experience with MySQL and PostgreSql - not with SQL Server. 但是,我只有MySQL和PostgreSql的经验 - 而不是SQL Server。

As to your DSN question - I'm not sure. 关于你的DSN问题 - 我不确定。

Good Luck! 祝好运!

If you are not set on PDO, use FreeTDS - aka procedural mssql_* calls. 如果您没有在PDO设置,使用freetds的 -又名程序mssql_ *通话。 This is one of the recommended work arounds until PDO is fixed . 在PDO 修复之前,这是推荐的解决方法之一。 Since PHP 5.1.2, FreeTDS has a mssql.charset-option . 从PHP 5.1.2开始, FreeTDS有一个mssql.charset-option

I noticed the same problem too, with an implementation of SQL server 2005 and PHP 5.x using PDO. 我也注意到了同样的问题,使用PDO实现了SQL Server 2005和PHP 5.x. When I changed nvarchar(MAX) field to nvarchar(255) the odd question mark characters stopped appearing. 当我将nvarchar(MAX)字段更改为nvarchar(255)时,奇数问号字符停止出现。 I definitely believe it has to do with the ODBC drivers in PDO and MS SQL server. 我绝对相信它与PDO和MS SQL服务器中的ODBC驱动程序有关。 When you implicitly specify the max characters in your varchar, it solves the problem. 当您隐式指定varchar中的最大字符时,它可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM