简体   繁体   English

与MS SQL Server 2008 R2的PHP连接

[英]PHP connection to MS SQL Server 2008 R2

I am implementing a PHP application that needs to be able to connect to both MS SQL Server 2012 and 2008 R2. 我正在实现一个PHP应用程序,该应用程序必须能够连接到MS SQL Server 2012和2008 R2。

Connection to 2012 works OK with this configuration 使用此配置可​​以正常连接到2012

  • Windows 7 64-bit Windows 7 64位
  • IIS 7.5 IIS 7.5
  • PHP 5.5.13 PHP 5.5.13
  • sql srv native client 11.0 sql srv本机客户端11.0
  • php_pdo_sqlsrv_55_nts.dll php_pdo_sqlsrv_55_nts.dll
  • php_sqlsrv_55_nts.dll php_sqlsrv_55_nts.dll

Connection to 2008 R2 does not work with this configuration: 与2008 R2的连接不适用于此配置:

  • Windows 7 64-bit Windows 7 64位
  • IIS 7.5 IIS 7.5
  • PHP 5.4.10 PHP 5.4.10
  • sql srv native client 10.5 sql srv本机客户端10.5
  • php_pdo_sqlsrv_54_nts.dll php_pdo_sqlsrv_54_nts.dll
  • php_sqlsrv_54_nts.dll php_sqlsrv_54_nts.dll

I get an error message saying that "This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server." 我收到一条错误消息,指出“此扩展名需要Microsoft SQL Server 2012本机客户端ODBC驱动程序才能与SQL Server通信。”

This is the code I use to connect. 这是我用来连接的代码。

class my_mssql{

protected $dsn;
protected $dbh;
protected $server;
protected $user;
protected $password;
protected $colnames;
public $error;
public $resultset;    

function __construct($db, $user, $password, $server='localhost'){

    $this->dsn = 'sqlsrv:Server='.$server.';Database='.$db;

    $this->server = $server;
    $this->user = $user;
    $this->password = $password;

    try {
        $this->dbh = new PDO($this->dsn, $user, $password);
    } catch (PDOException $e) {
        $this->error = $e->getMessage();
    }
}
}

It seems that php_pdo_sqlsrv_54_nts.dll supports only connection to 2012. Am I right? 似乎php_pdo_sqlsrv_54_nts.dll仅支持与2012的连接。对吗? On the other hand on the MS download page they say that the PHP driver v3.0 (containing php_pdo_sqlsrv_54_nts.dll) should support 2008 R2 as well. 另一方面,在MS下载页面上,他们说PHP驱动程序v3.0(包含php_pdo_sqlsrv_54_nts.dll)也应支持2008 R2。

Can I connect to 2008 R2 from PHP 5.4? 我可以从PHP 5.4连接到2008 R2吗? Should I maybe implement the 2008 R2 connection in a different way? 我是否应该以其他方式实现2008 R2连接?

I don't use the same driver as you, I use Mssql instead. 我使用的驱动程序与您不同,而是使用Mssql

I have a Windows Server 2008 R2 64 bit , and here is my working connection for that server: 我有一个Windows Server 2008 R2 64 bit ,这是该服务器的工作连接:

$this->dsn = 'mssql:host='.$server.';dbname='.$db;

$this->server = $server;
$this->user = $user;
$this->password = $password;

try {
    $this->dbh = new PDO($this->dsn ,$user, $password);
} catch (PDOException $e) {
    $this->error = $e->getMessage();
}

I honestly don't know the difference, there are so many ways and drivers to connect you know. 老实说,我不知道有什么区别,有很多方法和驱动程序可以用来连接。

OK it turns out that the information on the MS SQL Server 2012 Feature Pack (which contains the 2012 Native Client) Download page are somehow misleading. 好的,事实证明,MS SQL Server 2012功能包(包含2012本机客户端)下载页面上的信息在某种程度上具有误导性。

They say that "The Microsoft® SQL Server® 2012 Feature Pack is a collection of stand-alone packages which provide additional value for Microsoft® SQL Server® 2012." 他们说:“Microsoft®SQLServer®2012 Feature Pack是独立软件包的集合,可为Microsoft®SQLServer®2012提供附加价值。”

This seems to imply that the tools are intended for MS SQL Server 2012 only, but this is NOT the case. 这似乎暗示这些工具仅适用于MS SQL Server 2012,但事实并非如此。

The 2012 Native Client has to be used to connect to MS SQL 2008 R2 as well if you are using PHP >= 5.4 如果您使用的是PHP> = 5.4,则还必须使用2012 Native Client连接到MS SQL 2008 R2。

So this is the page with the correct information: http://technet.microsoft.com/en-us/library/cc296170(v=sql.105).aspx 因此,这是具有正确信息的页面: http : //technet.microsoft.com/zh-cn/library/cc296170(v=sql.105).aspx

In summary 综上所述

  • PHP < 5.4 use the 2.0 driver (Native client 2008) PHP <5.4使用2.0驱动程序(Native Client 2008)
  • PHP >= 5.4 use the 3.0 driver (Native client 2012) PHP> = 5.4使用3.0驱动程序(Native Client 2012)

this is valid for connections to BOTH MSSQL Server 2008 R2 and 2012 这对同时连接到MSSQL Server 2008 R2和2012均有效

DISCLAIMER: I haven't tested php < 5.4 connectivity to SQL Server 2012 免责声明:我尚未测试php <5.4与SQL Server 2012的连接性

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

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