首页 > 下载频道 > 编程相关 > SQL Server Driver for PHP srv4.0
SQL Server Driver for PHP srv4.0
授权形式: 免费版
更新时间: 2016-12-06 10:10:28
软件语言: 英文
软件平台: Win2000/WinXP/Win2003/win7/win8/win10
软件类别: 国外软件
文件大小: 未知
评论等级:
浏览次数: (今日:,本周:,本月:
软件简介

SQL Server Driver for PHP srv4.0

适用于php7

其中的52、53表示就是php的5.2.x和5.3.x 版本,选择跟你php版本相匹配的;vc6或vc9的选择要看你使用的是什么web服务器软件,如果使用的是IIS那就选择vc9的,如果是Apache 则选择vc6的,ts和nts的选择要看你安装的php版本是线程安全版的还是非线程安全版,ts是线程安全,nts是非线程安全。
如果不知道可以在phpinfo里看Zend Extension Build这个属性如下图:

\

2:将扩展拷贝到拷到php/ext目录下,在php.ini文件,添加一下代码:
extension=在ext下的pdo扩展(用于pdo)
extension=在ext下的扩展
3:重启服务器,打开phpinfo();看到以下状态就证明添加扩展成功,

\

4:连接测试:

1
2
3
4
5
6
7
8
9
10
11
12
  $serverName = "(local)";
  $connectionInfo = array("UID"=>"sa","PWD"=>"admin","Database"=>"db_online");
  $conn = sqlsrv_connect( $serverName, $connectionInfo);
  if( $conn ){
     echo "Connection established.\n";
  }else{
     echo "Connection could not be established.\n";
     die( var_dump(sqlsrv_errors()));
  }
  sqlsrv_close( $conn);
?>

注意这里的连接不是用mssql_connect而是用sqlsrv_connect,在这个版本中,还有几个函数:
这个扩展为php新增了一系列sqlsrv_开头的函数,常用的如下:

sqlsrv_connect
sqlsrv_close
sqlsrv_commit
sqlsrv_errors
sqlsrv_fetch
sqlsrv_fetch_array
sqlsrv_fetch_metadata
sqlsrv_num_rows
sqlsrv_query
sqlsrv_rollback
sqlsrv_rows_affected
. . .
另外注意的是,如果使用这个扩展连接Sql server 2005以及以上版本的sql server(如sql server 2008),你还需要在机器上先安装 SQL Server Native Client
不然会出现如下错误:

array
0 =>array
0 =>string'IMSSP'(length=5)
    'SQLSTATE' =>string'IMSSP'(length=5)
1 =>int-49
    'code' =>int-49
2 =>string'This extension requires the Microsoft SQL Server 2012 Native Client. Access the     following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712'(length=216)
'message' =>string'This extension requires the Microsoft SQL Server 2008 Native Client. Access the following URL to download the Microsoft SQL Server 2008 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712'(length=216)
1 =>array
0 =>string'IM002'(length=5)
    'SQLSTATE' =>string'IM002'(length=5)
1 =>int0
    'code' =>int0
2 =>string'[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序'(length=71)
'message' =>string'[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序'(length=71)
. . . .

解决方法:需要安装SQL Server 2008 Native Client ODBC Driver,下载安装文件sqlncli.msi,安装后就可以了。

示例应用程序是使用 Microsoft Drivers for PHP for SQL Server的 SQLSRV 驱动程序的 Web 应用程序。 该应用程序可使用户通过输入关键字来搜索产品、查看选定产品的评论、为选定产品撰写评论以及为选定产品上载图像。

运行示例应用程序

  1. 安装 Microsoft Drivers for PHP for SQL Server。 有关详细信息,请参阅PHP SQL 驱动程序入门
  2. 将文本档后面列出的代码复制到两个文件中:adventureworks_demo.php 和 photo.php。
  3. 将 adventureworks_demo.php 和 photo.php 文件放在 Web 服务器的根目录中。
  4. 通过从你的浏览器启动 http://localhost/adventureworks_demo.php 来运行应用程序。

 

示例
 
 

AdventureWorks 产品评论示例应用程序为名称包含用户输入的字符串的产品返回数据库中的产品信息。 从返回的产品列表中,用户可以查看评论、查看图像、上载图像和为选定产品撰写评论。

将以下代码放在名为 adventureworks_demo.php 的文件中:

 
 
  
  
  
  
  

AdventureWorks Product Reviews

This application is a demonstration of the procedural API (SQLSRV driver) of the Microsoft Drivers for PHP for SQL Server.

"AdventureWorks"); /* Connect using Windows Authentication. */ $conn = sqlsrv_connect( $serverName, $connectionOptions); if( $conn === false ) die( FormatErrors( sqlsrv_errors() ) ); if(isset($_REQUEST['action'])) { switch( $_REQUEST['action'] ) { /* Get AdventureWorks products by querying against the product name.*/ case 'getproducts': $params = array(&$_POST['query']); $tsql = "SELECT ProductID, Name, Color, Size, ListPrice FROM Production.Product WHERE Name LIKE '%' + ? + '%' AND ListPrice > 0.0"; /*Execute the query with a scrollable cursor so we can determine the number of rows returned.*/ $cursorType = array("Scrollable" => SQLSRV_CURSOR_KEYSET); $getProducts = sqlsrv_query($conn, $tsql, $params, $cursorType); if ( $getProducts === false) die( FormatErrors( sqlsrv_errors() ) ); if(sqlsrv_has_rows($getProducts)) { $rowCount = sqlsrv_num_rows($getProducts); BeginProductsTable($rowCount); while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC)) { PopulateProductsTable( $row ); } EndProductsTable(); } else { DisplayNoProdutsMsg(); } GetSearchTerms( !null ); /* Free the statement and connection resources. */ sqlsrv_free_stmt( $getProducts ); sqlsrv_close( $conn ); break; /* Get reviews for a specified productID. */ case 'getreview': GetPicture( $_GET['productid'] ); GetReviews( $conn, $_GET['productid'] ); sqlsrv_close( $conn ); break; /* Write a review for a specified productID. */ case 'writereview': DisplayWriteReviewForm( $_POST['productid'] ); break; /* Submit a review to the database. */ case 'submitreview': /*Prepend the review so it can be opened as a stream.*/ $comments = "data://text/plain,".$_POST['comments']; $stream = fopen( $comments, "r" ); $tsql = "INSERT INTO Production.ProductReview (ProductID, ReviewerName, ReviewDate, EmailAddress, Rating, Comments) VALUES (?,?,?,?,?,?)"; $params = array(&$_POST['productid'], &$_POST['name'], date("Y-m-d"), &$_POST['email'], &$_POST['rating'], &$stream); /* Prepare and execute the statement. */ $insertReview = sqlsrv_prepare($conn, $tsql, $params); if( $insertReview === false ) die( FormatErrors( sqlsrv_errors() ) ); /* By default, all stream data is sent at the time of query execution. */ if( sqlsrv_execute($insertReview) === false ) die( FormatErrors( sqlsrv_errors() ) ); sqlsrv_free_stmt( $insertReview ); GetSearchTerms( true ); /* Display a list of reviews, including the latest addition. */ GetReviews( $conn, $_POST['productid'] ); sqlsrv_close( $conn ); break; /* Display a picture of the selected product.*/ case 'displaypicture': $tsql = "SELECT Name FROM Production.Product WHERE ProductID = ?"; $getName = sqlsrv_query($conn, $tsql, array(&$_GET['productid'])); if( $getName === false ) die( FormatErrors( sqlsrv_errors() ) ); if ( sqlsrv_fetch( $getName ) === false ) die( FormatErrors( sqlsrv_errors() ) ); $name = sqlsrv_get_field( $getName, 0); DisplayUploadPictureForm( $_GET['productid'], $name ); sqlsrv_close( $conn ); break; /* Upload a new picture for the selected product. */ case 'uploadpicture': $tsql = "INSERT INTO Production.ProductPhoto (LargePhoto) VALUES (?); SELECT SCOPE_IDENTITY() AS PhotoID"; $fileStream = fopen($_FILES['file']['tmp_name'], "r"); $uploadPic = sqlsrv_prepare($conn, $tsql, array( array(&$fileStream, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max')))); if( $uploadPic === false ) die( FormatErrors( sqlsrv_errors() ) ); if( sqlsrv_execute($uploadPic) === false ) die( FormatErrors( sqlsrv_errors() ) ); /*Skip the open result set (row affected). */ $next_result = sqlsrv_next_result($uploadPic); if( $next_result === false ) die( FormatErrors( sqlsrv_errors() ) ); /* Fetch the next result set. */ if( sqlsrv_fetch($uploadPic) === false) die( FormatErrors( sqlsrv_errors() ) ); /* Get the first field - the identity from INSERT. */ $photoID = sqlsrv_get_field($uploadPic, 0); /* Associate the new photoID with the productID. */ $tsql = "UPDATE Production.ProductProductPhoto SET ProductPhotoID = ? WHERE ProductID = ?"; $reslt = sqlsrv_query($conn, $tsql, array(&$photoID, &$_POST['productid'])); if($reslt === false ) die( FormatErrors( sqlsrv_errors() ) ); GetPicture( $_POST['productid']); DisplayWriteReviewButton( $_POST['productid'] ); GetSearchTerms (!null); sqlsrv_close( $conn ); break; }//End Switch } else { GetSearchTerms( !null ); } function GetPicture( $productID ) { echo ""; echo ""; echo "
"; echo "
Upload new picture.

"; } function GetReviews( $conn, $productID ) { $tsql = "SELECT ReviewerName, CONVERT(varchar(32), ReviewDate, 107) AS [ReviewDate], Rating, Comments FROM Production.ProductReview WHERE ProductID = ? ORDER BY ReviewDate DESC"; /*Execute the query with a scrollable cursor so we can determine the number of rows returned.*/ $cursorType = array("Scrollable" => SQLSRV_CURSOR_KEYSET); $getReviews = sqlsrv_query( $conn, $tsql, array(&$productID), $cursorType); if( $getReviews === false ) die( FormatErrors( sqlsrv_errors() ) ); if(sqlsrv_has_rows($getReviews)) { $rowCount = sqlsrv_num_rows($getReviews); echo ""; echo "
$rowCount Reviews
"; while ( sqlsrv_fetch( $getReviews ) ) { $name = sqlsrv_get_field( $getReviews, 0 ); $date = sqlsrv_get_field( $getReviews, 1 ); $rating = sqlsrv_get_field( $getReviews, 2 ); /* Open comments as a stream. */ $comments = sqlsrv_get_field( $getReviews, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR)); DisplayReview($productID, $name, $date, $rating, $comments ); } } else { DisplayNoReviewsMsg(); } DisplayWriteReviewButton( $productID ); sqlsrv_free_stmt( $getReviews ); } /*** Presentation and Utility Functions ***/ function BeginProductsTable($rowCount) { /* Display the beginning of the search results table. */ $headings = array("Product ID", "Product Name", "Color", "Size", "Price"); echo ""; echo "$rowCount Results"; foreach ( $headings as $heading ) { echo ""; } echo ""; } function DisplayNoProdutsMsg() { echo "

No products found.

"; } function DisplayNoReviewsMsg() { echo "

There are no reviews for this product.

"; } function DisplayReview( $productID, $name, $date, $rating, $comments) { /* Display a product review. */ echo "
$heading
"; echo ""; echo "
ProductID Reviewer Date Rating
$productID $name $date $rating
"; fpassthru( $comments ); echo "


"; } function DisplayUploadPictureForm( $productID, $name ) { echo "

Upload Picture

"; echo "

$name

"; echo "
"; } function DisplayWriteReviewButton( $productID ) { echo "

 

 
"; } function DisplayWriteReviewForm( $productID ) { /* Display the form for entering a product review. */ echo "
Name, E-mail, and Rating are required fields.
"; echo "
 
Name:
E-mail:
Rating: 1 2 3 4 5

"; } function EndProductsTable() { echo "
"; } function GetSearchTerms( $success ) { /* Get and submit terms for searching the database. */ if (is_null( $success )) { echo "

Review successfully submitted.

";} echo "

Enter search terms to find products.

"; echo "
 
"; } function PopulateProductsTable( $values ) { /* Populate Products table with search results. */ $productID = $values['ProductID']; echo ""; foreach ( $values as $key => $value ) { if ( 0 == strcasecmp( "Name", $key ) ) { echo "$value"; } elseif( !is_null( $value ) ) { if ( 0 == strcasecmp( "ListPrice", $key ) ) { /* Format with two digits of precision. */ $formattedPrice = sprintf("%.2f", $value); echo "$$formattedPrice"; } else { echo "$value"; } } else { echo "N/A"; } } echo "
 
"; } function FormatErrors( $errors ) { /* Display errors. */ echo "Error information:
"; foreach ( $errors as $error ) { echo "SQLSTATE: ".$error['SQLSTATE']."
"; echo "Code: ".$error['code']."
"; echo "Message: ".$error['message']."
"; } } ?>

 

示例
 
 

photo.php 脚本返回指定的 ProductID的产品照片。 此脚本从 adventureworks_demo.php 脚本中调用。

将以下代码放在名为 photo.php 的文件中:

 
 
"AdventureWorks");  
  
/* Connect using Windows Authentication. */  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Get the product picture for a given product ID. */  
$tsql = "SELECT LargePhoto   
         FROM Production.ProductPhoto AS p  
         JOIN Production.ProductProductPhoto AS q  
         ON p.ProductPhotoID = q.ProductPhotoID  
         WHERE ProductID = ?";  
  
$params = array(&$_REQUEST['productId']);  
  
/* Execute the query. */  
$stmt = sqlsrv_query($conn, $tsql, $params);  
if( $stmt === false )  
{  
     echo "Error in statement execution.
";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Retrieve the image as a binary stream. */  
$getAsType = SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY);  
if ( sqlsrv_fetch( $stmt ) )  
{  
   $image = sqlsrv_get_field( $stmt, 0, $getAsType);  
   fpassthru($image);  
}  
else  
{  
     echo "Error in retrieving data.
";  
     die(print_r( sqlsrv_errors(), true));  
}  
  
/* Free the statement and connectin resources. */  
sqlsrv_free_stmt( $stmt );  
sqlsrv_close( $conn );  
?>  
 

下载地址
分享到:
推荐下载