PHP5.3利用PDO连接MSSQL2000,纠结好久,终于连接成功!


现将PDO成功连接MSSQL的步骤简要整理如下:


1、下载microsoft drivers  for php for sql server(目前有2.0 3.0版本,分别又分有ts和nts版,可通过phpinfo()中的Thread Safety:enable确认是ts,详见驱动自带手册)
2、复制驱动至php文件夹的ext文件夹后,修改php.ini扩展extension=php_pdo_sqlsrv_53_ts_vc6.dll
3、下载Microsoft SQL Server 2008 R2 Native Client安装
测试代码:


 try {
    $hostname = "192.168.1.100";
    $dbname = "Northwind";
    $username = "sa";
    $pwd = "pwd100";
    $dsn="sqlsrv:Server=$hostname;database=$dbname";

   $conn = new PDO ($dsn,$username,$pwd);
   $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
   echo "mssql database connnection sucessed!";
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }

?>