PHP操作SQL Server 2008/2012

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

 

二、操作示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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

posted @   52php  阅读(2265)  评论(0编辑  收藏  举报
编辑推荐:
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
阅读排行:
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路
点击右上角即可分享
微信分享提示