แก้ไข

แชร์ผ่าน


sqlsrv_num_fields

Download PHP driver

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 database are installed on the local computer. All output is written to the console when the example is run from the command line.

<?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));  
}  
  
/* 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

sqlsrv_field_metadata

About Code Examples in the Documentation