PHP操作SQL Server驱动,微软官方提供2个版本,Version 2.0 和 Version 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
2、PHP扩展 php_sqlsrv
Version 3.0 相关下载如下:
1、Microsoft® SQL Server® 2012 Native Client
2、PHP扩展 php_sqlsrv
以 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
二、操作示例
01
02
$dbHost ="192.168.137.99";
03
$uid ="snsgou.com";
04
$pwd ="snsgou08808";
05
$dbName ='sg_user';
06
$charset ='utf-8';
07
08
$connectionInfo =array("UID" =>$uid,"PWD" =>$pwd,"Database" =>$dbName,'CharacterSet' =>$charset);
09
$conn = sqlsrv_connect($dbHost,$connectionInfo);
10
11
if ($conn == false) {
12
echo "连接失败!";
13
die(print_r(sqlsrv_errors(), true));
14
}
15
16
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// 1.查询
18
sql ="SELECT TOP 10 * FROM t_user";
19
$query = sqlsrv_query($conn,$sql);
20
while ($row = sqlsrv_fetch_array($query)) {
21
echo $row['Id'] ."-----" .$row['Email'] ."
";
22
}
23
24
// 2.插入、更新
25
$sql ="insert into [SnsClass]([year],[month],[day],[className]) values ('2014','02','05', '初三(3)版')";
26
$query = sqlsrv_query($conn,$sql);
27
28
?>
参考: