欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP操作SQL Server 2008/2012

程序员文章站 2024-01-10 09:59:22
...

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 = "blog.snsgou.com";
$pwd = "snsgou08808";
$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