Udostępnij przez


Instrukcje: wysyłanie danych jako strumienia

Pobieranie sterownika PHP

Sterowniki firmy Microsoft dla języka PHP dla programu SQL Server wykorzystują strumienie PHP do wysyłania dużych obiektów na serwer. W przykładach w tym temacie pokazano, jak wysyłać dane jako strumień. W pierwszym przykładzie użyto sterownika SQLSRV, aby zademonstrować domyślne zachowanie, które polega na wysyłaniu wszystkich danych strumienia w czasie wykonywania zapytania. W drugim przykładzie użyto sterownika SQLSRV, aby zademonstrować sposób wysyłania do ośmiu kilobajtów (8 kB) danych strumienia jednocześnie na serwer.

W trzecim przykładzie pokazano, jak wysyłać strumień danych do serwera przy użyciu sterownika PDO_SQLSRV.

Przykład: Wysyłanie danych strumienia w trakcie wykonywania

Poniższy przykład wstawia wiersz do tabeli Production.ProductReview bazy danych AdventureWorks. Komentarze klienta ($comments) są otwierane jako strumień z funkcją fopen PHP, a następnie przesyłane strumieniowo do serwera po wykonaniu zapytania.

W tym przykładzie przyjęto założenie, że na komputerze lokalnym zainstalowano program SQL Server i bazę danych AdventureWorks . Wszystkie dane wyjściowe są zapisywane w konsoli.

<?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);  
?>  

Przykład: wysyłanie danych strumienia przy użyciu sqlsrv_send_stream_data

Następny przykład jest taki sam jak w poprzednim przykładzie, ale domyślne zachowanie wysyłania wszystkich danych strumienia podczas wykonywania jest wyłączone. W przykładzie użyto sqlsrv_send_stream_data do wysyłania danych strumieniowych do serwera. Do sqlsrv_send_stream_data wysyłane są maksymalnie osiem kilobajtów (8 kB) danych. Skrypt zlicza liczbę wywołań wykonanych przez sqlsrv_send_stream_data i wyświetla liczbę wywołań do konsoli.

W tym przykładzie przyjęto założenie, że na komputerze lokalnym zainstalowano program SQL Server i bazę danych AdventureWorks . Wszystkie dane wyjściowe są zapisywane na konsoli.

<?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);  
?>  

Mimo że przykłady w tym temacie wysyłają dane tekstowe do serwera, dane w każdym formacie mogą być wysyłane jako strumień. Można na przykład użyć technik przedstawionych w tym temacie do wysyłania obrazów w formacie binarnym jako strumieni.

Przykład: wysyłanie obrazu jako strumienia

<?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();  
?>  

Zobacz też

Aktualizowanie danych (sterowniki firmy Microsoft dla języka PHP dla programu SQL Server)

Pobieranie danych jako strumienia przy użyciu sterownika SQLSRV

Informacje o przykładach kodu w dokumentacji