Procedura: Inviare dati come flusso

Download del driver PHP

I driver Microsoft per PHP per SQL Server sfruttano i flussi PHP per l'invio di oggetti di grandi dimensioni al server. Negli esempi in questo argomento viene illustrato come inviare dati come flusso. Il primo esempio usa il driver SQLSRV per illustrare il comportamento predefinito, ovvero l'invio di tutti i dati di flusso al momento dell'esecuzione della query. Il secondo esempio usa il driver SQLSRV per illustrare come inviare fino a 8 KB di dati di flusso alla volta al server.

Il terzo esempio illustra come inviare i dati di flusso al server tramite il driver PDO_SQLSRV.

Esempio: invio di dati di flusso in fase di esecuzione

Nell'esempio seguente viene inserita una riga nella tabella Production.ProductReview del database AdventureWorks. I commenti dei clienti ($comments) vengono aperti come flusso con la funzione PHP fopen e quindi trasmessi al server durante l'esecuzione della query.

Nell'esempio si presuppone che SQL Server e il database AdventureWorks siano installati nel computer locale. Tutto l'output viene scritto nella console.

<?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 = "INSERT INTO Production.ProductReview (ProductID,   
                                               ReviewerName,  
                                               ReviewDate,  
                                               EmailAddress,  
                                               Rating,  
                                               Comments)  
         VALUES (?, ?, ?, ?, ?, ?)";  
  
/* Set the parameter values and put them in an array.  
Note that $comments is opened as a stream. */  
$productID = '709';  
$name = 'Customer Name';  
$date = date("Y-m-d");  
$email = 'customer@name.com';  
$rating = 3;  
$data = 'Insert any lengthy comment here.';
$comments = fopen('data:text/plain,'.urlencode($data), 'r');
$params = array($productID, $name, $date, $email, $rating, $comments);
  
/* Execute the query. All stream data is sent upon execution.*/  
$stmt = sqlsrv_query($conn, $tsql, $params);  
if ($stmt === false) {
     echo "Error in statement execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
} else {
     echo "The query was successfully executed.";  
}  
  
/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>  

Esempio: invio di dati di flusso tramite sqlsrv_send_stream_data

L'esempio successivo è uguale a quello precedente, ma il comportamento predefinito (invio di tutti i dati di flusso durante l'esecuzione) è disattivato. Per l'invio dei dati di flusso al server l'esempio usa sqlsrv_send_stream_data . Con ogni chiamata a sqlsrv_send_stream_data vengono inviati fino a 8 KB di dati. Lo script conta il numero di chiamate effettuate da sqlsrv_send_stream_data e visualizza il conteggio nella console.

Nell'esempio si presuppone che SQL Server e il database AdventureWorks siano installati nel computer locale. Tutto l'output viene scritto nella console.

<?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 = "INSERT INTO Production.ProductReview (ProductID,   
                                               ReviewerName,  
                                               ReviewDate,  
                                               EmailAddress,  
                                               Rating,  
                                               Comments)  
         VALUES (?, ?, ?, ?, ?, ?)";  
  
/* Set the parameter values and put them in an array.  
Note that $comments is opened as a stream. */  
$productID = '709';  
$name = 'Customer Name';  
$date = date("Y-m-d");  
$email = 'customer@name.com';  
$rating = 3;  
$data = 'Insert any lengthy comment here.';
$comments = fopen('data:text/plain,'.urlencode($data), 'r');
$params = array($productID, $name, $date, $email, $rating, $comments);  
  
/* Turn off the default behavior of sending all stream data at  
execution. */  
$options = array("SendStreamParamsAtExec" => 0);  
  
/* Execute the query. */  
$stmt = sqlsrv_query($conn, $tsql, $params, $options);  
if ($stmt === false) {
     echo "Error in statement execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Send up to 8K of parameter data to the server with each call to  
sqlsrv_send_stream_data. Count the calls. */  
$i = 1;  
while (sqlsrv_send_stream_data($stmt)) {
     echo "$i call(s) made.\n";  
     $i++;  
}  
  
/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>  

Negli esempi presentati in questo argomento vengono inviati al server dati di tipo carattere, ma è possibile inviare come flusso qualsiasi formato di dati. Ad esempio, è possibile usare le tecniche illustrate in questo argomento per inviare come flussi anche immagini in formato binario.

Esempio: invio di un'immagine come flusso

<?php  
   $server = "(local)";   
   $database = "Test";  
   $conn = new PDO( "sqlsrv:server=$server;Database = $database", "", "");  
  
   $binary_source = fopen( "data://text/plain,", "r");  
  
   $stmt = $conn->prepare("insert into binaries (imagedata) values (?)");  
   $stmt->bindParam(1, $binary_source, PDO::PARAM_LOB);   
  
   $conn->beginTransaction();  
   $stmt->execute();  
   $conn->commit();  
?>  

Vedi anche

Aggiornamento dei dati (Driver Microsoft per PHP per SQL Server)

Recupero di dati come flusso tramite il driver SQLSRV

Informazioni sugli esempi di codice nella documentazione