php 连接 sqlserver2005教程(sqlsrv扩展)

2013 年 4 月 22 日 at 下午 4:10分类:PHP

本人环境Apache:2.2.21 php:5.3.10 操作系统windows xp service pack 3

使用的是Microsoft Drivers for PHP for SQL Server的扩展包做连接。

1.下载Microsoft Drivers for PHP for SQL Server 扩展包  它分为2.0和3.0版

本。(本人使用的是2.0版本)

这个扩展包对电脑的操作系统有要求,对应的系统使用对应的版本。

3.0  所支持的操作系统
Windows Server 2008 R2 SP1
Windows Vista SP2
Windows Server 2008 SP2
Windows 7 SP1

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

所以本次实例使用的是 2.0  里面php_sqlsrv_53_ts_vc9.dll 扩展 ;

2.将php_sqlsrv_53_ts_vc9.dll文件复制到php的扩展文件夹 (一般的都在

php/ext文件夹下面);
3.将此php_sqlsrv_53_ts_vc9.dll文件拷贝到系统所在盘的system32中;
4.打开php.ini文件,在extension=php_mysql.dll 下面一行添加

extension=php_sqlsrv_53_ts_vc9.dll  之后重启apache。
5.测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$serverName = "127.0.0.1";
$uid = "sa";
$pwd = "000";
$db  = "php555";
 
$connectionInfo = array(
"UID"=$uid,"PWD"=$pwd,"Database"=$db
);
 
$conn = sqlsrv_connect( $serverName, $connectionInfo);
 
if( $conn === false )
{
echo "Unable to connect.";
die(var_dump( sqlsrv_errors(), true));
}else{
echo "sqlserver连接成功!!!";
}

6.运行上边代码会提示您以下内容
Unable to connect.
array(2) { [0]=> array(6) { [0]=> string(5) “IMSSP” ["SQLSTATE"]=>

string(5) “IMSSP” [1]=> int(-49) ["code"]=> int(-49) [2]=> string(390)

“This extension requires either the Microsoft SQL Server 2008 Native

Client (SP1 or later) or the Microsoft SQL Server 2008 R2 Native Client

ODBC Driver to communicate with SQL Server. Neither of those ODBC

Drivers are currently installed. Access the following URL to download

the Microsoft SQL Server 2008 R2 Native Client ODBC driver for x86:

http://go.microsoft.com/fwlink/?LinkId=163712″ ["message"]=> string

(390) “This extension requires either the Microsoft SQL Server 2008

Native Client (SP1 or later) or the Microsoft SQL Server 2008 R2 Native

Client ODBC Driver to communicate with SQL Server. Neither of those

ODBC Drivers are currently installed. Access the following URL to

download the Microsoft SQL Server 2008 R2 Native Client ODBC driver for

x86: http://go.microsoft.com/fwlink/?LinkId=163712″ } [1]=> array(6) {

[0]=> string(5) “IM002″ ["SQLSTATE"]=> string(5) “IM002″ [1]=> int(0)

["code"]=> int(0) [2]=> string(71) “[Microsoft][ODBC 驱动程序管理器] 未

发现数据源名称并且未指定默认驱动程序” ["message"]=> string(71)

“[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程

序” } } bool(true)

7.点击http://go.microsoft.com/fwlink/?LinkId=163712 这个链接 进入下载驱

动程序包就可以了,X86 Package(sqlncli.msi) 。安装的时候有可能某些软件会影响到安装,导致安装失败,

建议您关掉没用的程序。

8.之后再运行第5步代码; 提示:”sqlserver连接成功!!!” 。
9.具体的sqlsrv的函数就不做介绍了 在扩展包里面有手册,手册里面有详细介绍。

(转载请注明出处,谢谢)