1、测试环境
操作系统:Win7
数据库:SQL Server2005
脚本语言:php 5.3
2、下载SQLServerDriverForPHP1.1 这个驱动,下载后并解压.
3、把解压后的文件php_sqlsrv_53_nts_vc9,php_sqlsrv_53_ts_vc9拷贝到C:\Program Files\php\ext里.
注意:php_sqlsrv_53_nts_vc9(非线程安全),php_sqlsrv_53_ts_vc9(线程安全).
C:\Program Files\php\ext 此路径为安装或解压php后的路径
我查阅了下资料:如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本;而用FastCGI模式运行PHP的话就没有必要用线程安全检查了,用None Thread Safe(NTS,非线程安全)的版本能够更好的提高效率。
还有我们到底是用v6还是v9的dll,这个在php的官站上也有说明:
Which version do I choose?
If you are using PHP with Apache 1 or Apache2 from apache.org you need to use the VC6 versions of PHP [如果在apache服务器上,请用v6版本]
If you are using PHP with IIS you should use the VC9 versions of PHP [如果在iis里,请使用vc9版本]
VC6 Versions are compiled with the legacy Visual Studio 6 compiler
VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability. The VC9 versions require you to have the Microsoft 2008 C++ Runtime (x86) or the Microsoft 2008 C++ Runtime (x64) installed
Do NOT use VC9 version with apache.org binaries
VC9 versions of Apache can be fetched at Apache Lounge. We use their binaries to build the Apache SAPIs.
4、C:\Program Files\php\php.ini文件打开,找到如下代码:
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;
在上述代码下添加以下2行代码
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_sqlsrv_53_nts_vc9.dll
5、安装sql server2008的sqlncli.Msi(去微软官站下载)
6、重启iis
--------------------------------------------------------------------------
以下是测试代码 2012-11-08
参数:$serverName 服务器名称,$uid为用户名,$pwd为密码
--------------------------------------------------------------------------
$serverName = "(local)";
$uid = "sa";
$pwd = "123";
$connectionInfo = array("UID"=>$uid,"PWD"=>$pwd,"Database"=>"LK.SQLDB");
$conn = sqlsrv_connect($serverName,$connectionInfo);
if( $conn == false){
echo "连接失败!";
//die(print_r(sqlsrv_errors(), true));
}
else{
//echo "successful";
}
$tsql="select lID,[Name] from [LK_DiQu] order by lID desc";
$stmt = sqlsrv_query($conn,$tsql);
echo "<ul>";
while( $row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
echo '<li>'.$row["Name"].'</li>';
}
echo "<ul>";
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);