标签归档:sqlsrv

PHP 连接 SQLServer2005/2008/2012/2014(For Win)

PHP连接SQLServer2005/2008或以后版本数据库与以往的连接SQLServer2000是不一样的,需要自己添加PHP对SQLServer连接的驱动扩展。

(php.ini中的extension=php_mssql.dll扩展只适用连接于SQLServer2000)

1、微软官网下载一个SQLServer Driver for PHP的扩展包。下载地址
2、安装解压缩,解压出来一堆DLL文件,全部放到php\ext目录下
3、在php.ini的Dynamic Extensions中添加如下两条扩展:

    extension=php_sqlsrv_56_ts.dll
    extension=php_pdo_sqlsrv_56_ts.dll

4、重启apache
5、测试代码

<?php
  $servern="SFKFK27EL8FJ\SQLTRY";
  $coninfo=array("Database"=>"db address","UID"=>"sa","PWD"=>"123456");
  $conn=sqlsrv_connect($servern,$coninfo) or die ("连接失败!");
  $val=sqlsrv_query($conn,"select * from usertable");
  while($row=sqlsrv_fetch_array($val)){
    echo $row[1]."<br />";
  }
  sqlsrv_close($conn);
?>