PHP5.3利用PDO连接MSSQL

家电维修 2023-07-16 19:17www.caominkang.com家电维修技术

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 = "Northind";
    $username = "sa";
    $pd = "pd100";
    $dsn="sqlsrv:Server=$hostname;database=$dbname";

   $conn = ne PDO ($dsn,$username,$pd);
   $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;
  }

?>

Copyright © 2016-2025 www.jianfeikang.com 建飞家电维修 版权所有 Power by