不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2014-09-09 14:58  分类:Windows服务器  编辑

PHP操作SQL Server驱动,微软官方提供2个版本,Version 2.0Version 3.0 

Version 2.0 版本支持的操作系统有:

  • Windows Server 2003 Service Pack 1
  • Windows XP Service Pack 3
  • Windows Vista Service Pack 1 or later
  • Windows Server 2008
  • Windows Server 2008 R2
  • Windows 7

Version 3.0 版本支持的操作系统有:

  • Windows Server 2008 R2 SP1
  • Windows Vista SP2
  • Windows Server 2008 SP2
  • Windows 7 SP1

 

Version 2.0 版本支持PHP版本如下:

Driver file

PHP version

Thread safe?

Use with PHP .dll

php_sqlsrv_53_nts_vc6.dll

php_pdo_sqlsrv_53_nts_vc6.dll

5.3

no

php5.dll

php_sqlsrv_53_nts_vc9.dll

php_pdo_sqlsrv_53_nts_vc9.dll

5.3

no

php5.dll

php_sqlsrv_53_ts_vc6.dll

php_pdo_sqlsrv_53_ts_vc6.dll

5.3

yes

php5ts.dll

php_sqlsrv_53_ts_vc9.dll

php_pdo_sqlsrv_53_ts_vc9.dll

5.3

yes

php5ts.dll

php_sqlsrv_52_nts_vc6.dll

php_pdo_sqlsrv_52_nts_vc6.dll

5.2

no

php5.dll

php_sqlsrv_52_ts_vc6.dll

php_pdo_sqlsrv_52_ts_vc6.dll

5.2

yes

php5ts.dll

 

Version 3.0 版本支持PHP版本如下:

Driver file

PHP version

Thread safe?

Use with PHP .dll

php_sqlsrv_53_nts.dll

php_pdo_sqlsrv_53_nts.dll

5.3

no

php5.dll

php_sqlsrv_53_ts.dll

php_pdo_sqlsrv_53_ts.dll

5.3

yes

php5ts.dll

php_sqlsrv_54_nts.dll

php_pdo_sqlsrv_54_nts.dll

5.4

no

php5.dll

php_sqlsrv_54_ts.dll

php_pdo_sqlsrv_54_ts.dll

5.4

yes

php5ts.dll

 

让PHP能正常的操作SQL Server,除了上述的PHP扩展,还需要安装 SQL Server Native Client,Version 2.0 需要 Microsoft® SQL Server® 2008 Native Client,Version 3.0 需要 Microsoft® SQL Server® 2012 Native Client。

Version 2.0 相关下载如下:

1、Microsoft® SQL Server® 2008 Native Client

[微软官方]下载:X86版本   X64版本

[CSDN]下载:http://download.csdn.net/detail/wenjianbao/7884885

2、PHP扩展 php_sqlsrv

[微软官方]下载:http://www.microsoft.com/en-us/download/details.aspx?id=20098

[CSDN]下载:http://download.csdn.net/detail/wenjianbao/7884885

 

Version 3.0 相关下载如下:

1、Microsoft® SQL Server® 2012 Native Client

[微软官方]下载:X86版本   X64版本

[CSDN]下载:http://download.csdn.net/detail/wenjianbao/7884903

2、PHP扩展 php_sqlsrv

[微软官方]下载:http://www.microsoft.com/en-us/download/details.aspx?id=20098

[CSDN]下载:http://download.csdn.net/detail/wenjianbao/7884903

 

 


 

 

以 PHP5.3.29 操作SQL Server2008 为例,当前操作系统为 Windows 7 sp1(64位):

一、软件安装

1、下载安装 X64版本 的 Microsoft® SQL Server® 2012 Native Client

2、下载 Version 3.0 相关软件。我本地用的是 xampp运行PHP,即 Apache以非fcgi模式运行PHP,故需要选择 “线程安全版”的 php_sqlsrv_53_ts.dll,并拷贝到 PHP的ext目录。

在 php.ini 中配置下扩展:

extension=php_sqlsrv_53_ts.dll

 

二、操作示例

<?php
$dbHost = "192.168.137.99";
$uid = "php-note.com";
$pwd = "phpnote08808";
$dbName = 'sg_user';
$charset = 'utf-8';

$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => $dbName, 'CharacterSet' => $charset);
$conn = sqlsrv_connect($dbHost, $connectionInfo);

if ($conn == false) {
	echo "连接失败!";
	die(print_r(sqlsrv_errors(), true));
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 1.查询
$sql = "SELECT TOP 10 * FROM t_user";
$query = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($query)) {
	echo $row['Id'] . "-----" . $row['Email'] . "<br/>";
}

// 2.插入、更新
$sql = "insert into [SnsClass]([year],[month],[day],[className]) values ('2014','02','05', '初三(3)版')";
$query = sqlsrv_query($conn, $sql);

?>

 

参考:

System Requirements (Microsoft Drivers for PHP for SQL Server)

SQLSRV Driver API Reference

 

 


 

PHP 5.6 + Nginx + SQL Server 2008/2012

主要参考 官方教程

System Requirements for the Microsoft Drivers for PHP for SQL Server

Download the Microsoft Drivers for PHP for SQL Server

Windows

The following versions of the drivers for PHP on Windows are available for download:

在 php.ini 中配置下扩展:

extension=php_sqlsrv_56_nts.dll

温馨提示:phpStudy 2018 版本,里面自带了 SqlServer 访问扩展,无需再下载,在 php.ini 里开启一下相应的dll即可!