Procedura: Recuperare dati di tipo carattere come flusso usando il driver SQLSRV

Download del driver PHP

Il recupero di dati come flusso è disponibile solo nel driver SQLSRV dei driver di Microsoft per PHP per SQL Server e non è disponibile nel driver PDO_SQLSRV.

Il driver SQLSRV sfrutta i flussi PHP per recuperare grandi quantità di dati dal server. Nell'esempio riportato in questo argomento viene illustrato come recuperare i dati di tipo carattere come flusso.

Esempio

Nell'esempio seguente viene recuperata una riga dalla tabella Production.ProductReview del database AdventureWorks. Il campo Comments della riga restituita viene recuperato come flusso e visualizzato tramite la funzione PHP fpassthru.

Il recupero di dati come flusso viene eseguito usando sqlsrv_fetch e sqlsrv_get_field specificando flusso di caratteri come tipo restituito. Il tipo restituito viene specificato usando la costante SQLSRV_PHPTYPE_STREAM. Per informazioni sulle costanti di sqlsrv, vedere Costanti (driver Microsoft per PHP per SQL Server).

Nell'esempio si presuppone che SQL Server e il database AdventureWorks siano installati nel computer locale. Quando si esegue l'esempio dalla riga di comando, tutto l'output viene scritto nel browser.

<?php  
/*Connect to the local server using Windows Authentication and  
specify the AdventureWorks database as the database in use. */  
$serverName = "(local)";  
$connectionInfo = array( "Database"=>"AdventureWorks");  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Set up the Transact-SQL query. */  
$tsql = "SELECT ReviewerName,   
               CONVERT(varchar(32), ReviewDate, 107) AS [ReviewDate],  
               Rating,   
               Comments   
         FROM Production.ProductReview   
         WHERE ProductReviewID = ? ";  
  
/* Set the parameter value. */  
$productReviewID = 1;  
$params = array( $productReviewID);  
  
/* Execute the query. */  
$stmt = sqlsrv_query($conn, $tsql, $params);  
if( $stmt === false )  
{  
     echo "Error in statement execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Retrieve and display the data. The first three fields are retrieved  
as strings and the fourth as a stream with character encoding. */  
if(sqlsrv_fetch( $stmt ) === false )  
{  
     echo "Error in retrieving row.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
echo "Name: ".sqlsrv_get_field( $stmt, 0 )."\n";  
echo "Date: ".sqlsrv_get_field( $stmt, 1 )."\n";  
echo "Rating: ".sqlsrv_get_field( $stmt, 2 )."\n";  
echo "Comments: ";  
$comments = sqlsrv_get_field( $stmt, 3,   
                             SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));  
fpassthru($comments);  
  
/* Free the statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>  

Poiché per i primi tre campi non è stato specificato alcun tipo restituito PHP, ogni campo viene restituito in base al proprio tipo PHP predefinito. Per informazioni sui tipi di dati PHP predefiniti, vedere Default PHP Data Types. Per informazioni su come specificare i tipi restituiti PHP, vedere How to: Specify PHP Data Types.

Vedi anche

Recupero di dati

Recupero di dati come flusso tramite il driver SQLSRV

Informazioni sugli esempi di codice nella documentazione