--- title: "sqlsrv_num_fields | Microsoft Docs" ms.custom: "" ms.date: "03/23/2017" ms.prod: sql ms.prod_service: connectivity ms.reviewer: "" ms.technology: connectivity ms.topic: conceptual apiname: - "sqlsrv_num_fields" apitype: "NA" helpviewer_keywords: - "sqlsrv_num_fields" - "API Reference, sqlsrv_num_fields" ms.assetid: 03ca1860-01ed-408c-862a-57a7355de4bf author: David-Engel ms.author: v-daenge --- # sqlsrv_num_fields [!INCLUDE[Driver_PHP_Download](../../includes/driver_php_download.md)] Retrieves the number of fields in an active result set. This function can be called on any prepared statement, before or after execution. ## Syntax ``` sqlsrv_num_fields( resource $stmt) ``` #### Parameters *$stmt*: The statement on which the targeted result set is active. ## Return Value An integer value that represents the number of fields in the active result set. If an error occurs, the Boolean value **false** is returned. ## Example The following example executes a query to retrieve all fields for the top three rows in the *HumanResources.Department* table of the AdventureWorks database. The **sqlsrv_num_fields** function determines the number of fields in the result set. This allows data to be displayed by iterating through the fields in each returned row. The example assumes that SQL Server and the [AdventureWorks](https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/adventure-works) database are installed on the local computer. All output is written to the console when the example is run from the command line. ``` "AdventureWorks"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "Could not connect.\n"; die( print_r( sqlsrv_errors(), true)); } /* Define and execute the query. */ $tsql = "SELECT TOP (3) * FROM HumanResources.Department"; $stmt = sqlsrv_query($conn, $tsql); if( $stmt === false) { echo "Error in executing query.\n"; die( print_r( sqlsrv_errors(), true)); } /* Retrieve the number of fields. */ $numFields = sqlsrv_num_fields( $stmt ); /* Iterate through each row of the result set. */ while( sqlsrv_fetch( $stmt )) { /* Iterate through the fields of each row. */ for($i = 0; $i < $numFields; $i++) { echo sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." "; } echo "\n"; } /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt ); sqlsrv_close( $conn ); ?> ``` ## See Also [SQLSRV Driver API Reference](../../connect/php/sqlsrv-driver-api-reference.md) [sqlsrv_field_metadata](../../connect/php/sqlsrv-field-metadata.md) [About Code Examples in the Documentation](../../connect/php/about-code-examples-in-the-documentation.md)