隐藏

PHP連MSSQL設定

发布:2014/7/8 11:26:45作者:管理员 来源:本站 浏览次数:1122

這個寒假也算是惡搞吧,整人整到自己跳下來解,所謂報應還是求知心就不知了.....

 因為本人對ASP.NET比較有好感,對於PHP只能說是入門階段而已(新手階段)

 分配完工作後,學弟既然用IIS+PHP5.3.x+MYSQL後來就跟他說.....你換個資料MSSQL玩玩看

 沒多久就掛了XD....卡的問題好像是php_mssql.dll - 找不到指定的模組與php-cgi.exe找不到輸入程序

 後來自己去測試好像也卡在這兩個問題…….

 

php-cgi.exe比較簡單解決,後來比較悶的是php_mssql.dll的問題(似乎5.2.x就不太支援了)

 一直無法解決後來,爬文找到似乎微軟有釋出SQLServerDriverForPHP11....感動

 延伸上次的環境IIS7.0執行PHP

 

小改部分是中規中局的把資料夾名稱改回C:\PHP當組態設定部分也要跟著小改

 首先要下載http://www.dlldll.com/php5ts.dll_download.html

丟在C:/PHP內即可..............

 

進入IIS設定ISAP及CGI限制

image

 

設定完按確定後記得要允許

image

 image

 

之後直接上SQL Server Driver for PHP Version 1.1

 點擊安裝會問你要壓縮到哪,直接選在C:\PHP\ext內即可

接著點開上次安裝的PHP Manager點擊Enable or disable an extension

image

  

這邊有分

52     53      是指 5.2.x              5.3.x

nts     ts       是指 Thread Safe   NonThread Safe

vc6    vc9    是指 Apache         IIS

image

 正常的話開啟後會看到這些選項...它是對設定檔上的Register new PHP version設定的資料夾

並按照你的版本號設定,我測試的版本是5.3.5 的NTS

選取php_sqlsrv_53_nts_vc9.dll  按右鍵Enable 後,重新啟動IIS

image

在進入PHP Manager 內觀看check phpinfo()

image

成功後因該會多一個Sqlsrv資訊欄...

image

如果資料庫是MSSQL 2008還要去下載Microsoft SQL Server 2008 Native Client (sqlncli10.dll),請下載與環境對應的版本。

 

01     <?php
02 header('Content-type: text/html; charset=utf-8');
03 /* Specify the server and connection string attributes. */
04 $serverName = "127.0.0.1";//資料庫位子
05  
06 /* Get UID and PWD from application-specific files.  */
07 $uid = "joe80075"; //帳號
08  $pwd = "123456";        //密碼
09 $connectionInfo = array( "UID"=>$uid,"PWD"=>$pwd,"Database"=>"DA_DATABASE","CharacterSet" => "UTF-8");
10  
11 /* Connect using SQL Server Authentication. */
12 //sqlsrv_connect( string $serverName [, array $connectionInfo])
13 $conn = sqlsrv_connect( $serverName, $connectionInfo);
14 if( $conn === false )
15 {
16 echo "Unable to connect.<br />";
17 die( print_r( sqlsrv_errors(), true));
18 }
19  
20 /* Query SQL Server for the login of the user accessing the
21 database. */
22  
23 $tsql = "SELECT * from book";
24 $stmt = sqlsrv_query( $conn, $tsql);
25  
26 if( $stmt === false )
27 {
28 echo "Error in executing query.&lt;/br&gt;";
29 die( print_r( sqlsrv_errors(), true));
30 }
31  
32 /* Retrieve and display the results of the query. */
33 $row = sqlsrv_fetch_array($stmt);
34 echo $row[0]."<br />";
35 echo $row[1]."<br />";
36 /* Free statement and connection resources. */
37 sqlsrv_free_stmt( $stmt);
38 sqlsrv_close( $conn);
39 ?>

 

測試畫面

image