sqlsrv_get_field

Pobieranie sterownika PHP

Pobiera dane z określonego pola bieżącego wiersza. Dane terenowe muszą być dostępne w kolejności. Na przykład dane z pierwszego pola nie mogą być dostępne po uzyskaniu dostępu do danych z drugiego pola.

Syntax

sqlsrv_get_field( resource $stmt, int $fieldIndex [, int $getAsType])  

Parametry

$stmt: Zasób instrukcji odpowiadający wykonanemu zawołaniu.

$fieldIndex: Indeks pola do odzyskania. Indeksy zaczynają się od zera.

$getAsType [OPCJONALNIE]: stała sqlsrv (SQLSRV_PHPTYPE_*), która określa typ danych PHP dla zwracanych danych. Informacje o obsługiwanych typach danych można znaleźć w artykule Constants (Microsoft Drivers for PHP for SQL Server). Jeśli nie podano typu zwrotu, zostanie zwrócony domyślny typ PHP. Informacje o domyślnych typach PHP można znaleźć w artykule Domyślne typy danych PHP. Aby uzyskać informacje o określaniu typów danych PHP, zobacz: Jak określić typy danych PHP.

Wartość zwracana

Dane terenowe. Możesz określić typ danych PHP dla zwróconych danych, używając parametru $getAsType . Jeśli nie podano typu danych zwrotnych, zwrócony zostanie domyślny typ danych PHP. Informacje o domyślnych typach PHP można znaleźć w artykule Domyślne typy danych PHP. Aby uzyskać informacje o określaniu typów danych PHP, zobacz: Jak określić typy danych PHP.

Remarks

Połączenie sqlsrv_fetch i sqlsrv_get_field zapewnia dostęp do danych wyłącznie w bezpośrednim kierunku.

Połączenie sqlsrv_fetch/sqlsrv_get_field ładuje tylko jedno pole wiersza zbioru wyników do pamięci skryptów i pozwala na specyfikację typu zwrotu w PHP. (Informacje o tym, jak określić typ zwrotu PHP, znajdziesz w artykule Jak określić: Określić typy danych PHP.) To połączenie funkcji pozwala również na pobieranie danych jako strumienia. (Informacje o pobieraniu danych jako strumienia można znaleźć w artykule Retrieving Data as a stream Using the SQLSRV Driver.)

Example

Poniższy przykład pobiera wiersz danych zawierający recenzję produktu oraz nazwisko recenzenta. Aby pobrać dane z zestawu wyników, używa się sqlsrv_get_field . 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, gdy przykład jest uruchamiany z wiersza polecenia.

<?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 and execute the query. Note that both ReviewerName and  
Comments are of the SQL Server nvarchar type. */  
$tsql = "SELECT ReviewerName, Comments   
         FROM Production.ProductReview  
         WHERE ProductReviewID=1";  
$stmt = sqlsrv_query( $conn, $tsql);  
if( $stmt === false )  
{  
     echo "Error in statement preparation/execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Make the first row of the result set available for reading. */  
if( sqlsrv_fetch( $stmt ) === false )  
{  
     echo "Error in retrieving row.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Note: Fields must be accessed in order.  
Get the first field of the row. Note that no return type is  
specified. Data will be returned as a string, the default for  
a field of type nvarchar.*/  
$name = sqlsrv_get_field( $stmt, 0);  
echo "$name: ";  
  
/*Get the second field of the row as a stream.  
Because the default return type for a nvarchar field is a  
string, the return type must be specified as a stream. */  
$stream = sqlsrv_get_field( $stmt, 1,   
                            SQLSRV_PHPTYPE_STREAM( SQLSRV_ENC_CHAR));  
while( !feof( $stream))  
{   
    $str = fread( $stream, 10000);  
    echo $str;  
}  
  
/* Free the statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>