sqlsrv_fetch

Descargar controlador PHP

Hace que la siguiente fila de un conjunto de resultados esté disponible para la lectura. Utilice sqlsrv_get_field para leer campos de la fila.

Sintaxis

  
sqlsrv_fetch( resource $stmt[, row[, ]offset])  

Parámetros

$stmt: un recurso de instrucción correspondiente a una instrucción ejecutada.

Nota

Se debe ejecutar una instrucción antes de poder recuperar resultados. Para obtener información sobre cómo ejecutar una instrucción, consulte sqlsrv_query y sqlsrv_execute.

row (opcional): uno de los siguientes valores; sirve para especificar la fila a la que se accederá en un conjunto de resultados que use un cursor desplazable:

  • SQLSRV_SCROLL_NEXT

  • SQLSRV_SCROLL_PRIOR

  • SQLSRV_SCROLL_FIRST

  • SQLSRV_SCROLL_LAST

  • SQLSRV_SCROLL_ABSOLUTE

  • SQLSRV_SCROLL_RELATIVE

Para obtener más información sobre estos valores, vea Especificación de un tipo de cursor y selección de filas.

offset [OPCIONAL]: Se usa con SQLSRV_SCROLL_ABSOLUTE y SQLSRV_SCROLL_RELATIVE para especificar la fila que se va a recuperar. El primer registro del conjunto de resultados es 0.

Valor devuelto

Si se ha recuperado correctamente la siguiente fila del conjunto de resultados, se devuelve el valor True . Si no hay más resultados en el conjunto de resultados, se devuelve el valor Null . Si se produce un error, se devuelve el valor False .

Ejemplo

En el ejemplo siguiente se usa sqlsrv_fetch para recuperar una fila de datos que contiene una reseña del producto y el nombre del autor. Para recuperar datos del conjunto de resultados, se usa sqlsrv_get_field. En el ejemplo se da por hecho que SQL Server y la base de datos AdventureWorks están instalados en el equipo local. Los resultados se agregan a la consola cuando se ejecuta el ejemplo en la línea de comandos.

<?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 SQL Server type nvarchar. */  
$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);  
?>  

Consulte también

Recuperación de datos

Referencia de API del controlador SQLSRV

Sobre los ejemplos de código de la documentación