Labels

2005 (136) 2004 (99) 2006 (84) 2007 (41) 2008 (41) 簡單生活 (24) 2012 (14) 網頁設計 (14) 電腦技巧 (13) 2009 (12) 2010 (10) PHP (10) 台灣晃一晃 (9) Learn Note (7) 地球這麼大 (6) 2011 (5) Mysql (3) Smarty (3) Vista (3) 手機待吐 (2) 2014 (1) 2021 (1) Composer (1) Laravel (1) MAC (1) MAMP (1) 新新人類新科技 (1)

20130313

php 5.3.x 連ms sql server

php在5.3以後不支援mssql extension了,但還好微軟有提供sqlsrv套件來支援,

請依照下面步驟來配置:或參考微軟官方網頁

1.先確認自己的系統是否為以下其中之一:
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.下載sqlsrv套件:下載SQLSRV20.EXE,若是WIN7則下載SQLSRV30.EXE
下載後解壓縮可以看到許多dll檔案,這時請打開您的phpinfo頁面,查看
Zend Extension Build,我的編制為:API220090626,TS,VC9,故請將壓縮後的dll檔,其後面有ts_vc9的檔案附至到php預設的ext資料夾裡,並且配置php.ini:
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll 
3.下載Microsoft SQL Server 2008 R2 Native Client:XP用戶點選 Download the X86 package,更進階64為原則點選Download the X64 package

安裝完後重新啟動web server ,並且撰寫測試ms sql server 的程式碼:
 $DB_HOST1        = "";//IP   
 $DB_LOGIN1       = "";//uid
 $DB_PASSWORD1    = "";//pwd
 $DB_NAME1        = "";//db name

 $serverName = $DB_HOST1;
 $connectionInfo = array("UID"=>$DB_LOGIN1, "PWD"=>$DB_PASSWORD1, "Database"=>$DB_NAME1, "CharacterSet" => "UTF-8" );
 $conn = sqlsrv_connect( $serverName, $connectionInfo);

 $params = array();
 $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
 $sqlstr = "select * from Account";
 $res = sqlsrv_query($conn,$sqlstr,$params,$options);
 $num = sqlsrv_num_rows($res);
 for($i=0;$i<$num;$i++)
 {
  $r = sqlsrv_fetch_array($res);
  echo $r[0].' '.$r[1].'
';
 }
?>
 順利列出資料表則大功告成唷!!