Share via


sqlsrv_get_field

Scaricare il driver PHP

Recupera i dati dal campo specificato della riga corrente. L'accesso ai dati dei campi deve avvenire in successione. Ad esempio, non è possibile accedere ai dati del primo campo dopo aver eseguito l'accesso ai dati del secondo campo.

Sintassi

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

Parametri

$stmt: risorsa di istruzione corrispondente a un'istruzione eseguita.

$fieldIndex: indice del campo da recuperare. Gli indici iniziano da zero.

$getAsType [FACOLTATIVO]: costante SQLSRV (SQLSRV_PHPTYPE_*) che determina il tipo di dati PHP per i dati restituiti. Per informazioni sui tipi di dati supportati, vedere Costanti (driver Microsoft per PHP per SQL Server). Se non viene specificato alcun tipo restituito, verrà restituito un tipo PHP predefinito. Per informazioni sui tipi PHP predefiniti, vedere Default PHP Data Types. Per informazioni sulla specifica dei tipi di dati PHP, vedere How to: Specify PHP Data Types.

Valore restituito

I dati del campo. È possibile specificare il tipo di dati PHP dei dati restituiti usando il parametro $getAsType . Se non viene specificato alcun tipo di dati restituito, verrà restituito il tipo di dati PHP predefinito. Per informazioni sui tipi PHP predefiniti, vedere Default PHP Data Types. Per informazioni sulla specifica dei tipi di dati PHP, vedere How to: Specify PHP Data Types.

Osservazioni

La combinazione di sqlsrv_fetch e sqlsrv_get_field fornisce accesso forward-only ai dati.

La combinazione di sqlsrv_fetch/sqlsrv_get_field carica un solo campo di una riga del set di risultati nella memoria script e consente di specificare il tipo PHP restituito. Per informazioni su come specificare il tipo PHP restituito, vedere Procedura: Specificare i tipi di dati PHP. Questa combinazione di funzioni consente inoltre di recuperare i dati come flusso. Per informazioni sul recupero di dati come flusso, vedere Recupero di dati come flusso usando il driver SQLSRV.

Esempio

L'esempio seguente recupera una riga di dati contenente una revisione di prodotto e il nome del revisore. Per recuperare i dati dal set di risultati, è possibile usare sqlsrv_get_field. 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 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);  
?>  

Vedere anche

Riferimento all'API del driver SQLSRV

Recupero di dati

Informazioni sugli esempi di codice nella documentazione