简体   繁体   中英

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.

Connection to 2012 works OK with this configuration

  • Windows 7 64-bit
  • IIS 7.5
  • PHP 5.5.13
  • sql srv native client 11.0
  • php_pdo_sqlsrv_55_nts.dll
  • php_sqlsrv_55_nts.dll

Connection to 2008 R2 does not work with this configuration:

  • Windows 7 64-bit
  • IIS 7.5
  • PHP 5.4.10
  • sql srv native client 10.5
  • php_pdo_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."

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? 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.

Can I connect to 2008 R2 from PHP 5.4? Should I maybe implement the 2008 R2 connection in a different way?

I don't use the same driver as you, I use Mssql instead.

I have a Windows Server 2008 R2 64 bit , and here is my working connection for that server:

$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.

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."

This seems to imply that the tools are intended for MS SQL Server 2012 only, but this is NOT the case.

The 2012 Native Client has to be used to connect to MS SQL 2008 R2 as well if you are using PHP >= 5.4

So this is the page with the correct information: http://technet.microsoft.com/en-us/library/cc296170(v=sql.105).aspx

In summary

  • PHP < 5.4 use the 2.0 driver (Native client 2008)
  • PHP >= 5.4 use the 3.0 driver (Native client 2012)

this is valid for connections to BOTH MSSQL Server 2008 R2 and 2012

DISCLAIMER: I haven't tested php < 5.4 connectivity to SQL Server 2012

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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