PHP操作SQL Server 2008/2012

weixin_34126215發表於2016-07-16

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

相關文章