DownloadDownload PHP Driver

Retrieves metadata for the fields of a prepared statement. For information about preparing a statement, see sqlsrv_query or sqlsrv_prepare. Note that sqlsrv_field_metadata can be called on any prepared statement, pre- or post-execution.

sqlsrv_field_metadata( resource $stmt)  


$stmt: A statement resource for which field metadata is sought.

An array of arrays or false. The array consists of one array for each field in the result set. Each sub-array has keys as described in the table below. If there is an error in retrieving field metadata, false is returned.

NameName of the column to which the field corresponds.
TypeNumeric value that corresponds to a SQL type.
SizeNumber of characters for fields of character type (char(n), varchar(n), nchar(n), nvarchar(n), XML). Number of bytes for fields of binary type (binary(n), varbinary(n), UDT). NULL for other SQL Server data types.
PrecisionThe precision for types of variable precision (real, numeric, decimal, datetime2, datetimeoffset, and time). NULL for other SQL Server data types.
ScaleThe scale for types of variable scale (numeric, decimal, datetime2, datetimeoffset, and time). NULL for other SQL Server data types.
NullableAn enumerated value indicating whether the column is nullable (SQLSRV_NULLABLE_YES), the column is not nullable (SQLSRV_NULLABLE_NO), or it is not known if the column is nullable (SQLSRV_NULLABLE_UNKNOWN).

The following table gives more information on the keys for each sub-array (see the SQL Server documentation for more information on these types):

SQL Server 2008 data typeTypeMin/Max PrecisionMin/Max ScaleSize
bigintSQL_BIGINT (-5)8
binarySQL_BINARY (-2)0 < n < 8000 1
bitSQL_BIT (-7)
charSQL_CHAR (1)0 < n < 8000 1
dateSQL_TYPE_DATE (91)10/100/0
datetimeSQL_TYPE_TIMESTAMP (93)23/233/3
datetime2SQL_TYPE_TIMESTAMP (93)19/270/7
datetimeoffsetSQL_SS_TIMESTAMPOFFSET (-155)26/340/7
decimalSQL_DECIMAL (3)1/380/precision value
floatSQL_FLOAT (6)4/8
moneySQL_DECIMAL (3)19/194/4
ncharSQL_WCHAR (-8)0 < n < 4000 1
numericSQL_NUMERIC (2)1/380/precision value
nvarcharSQL_WVARCHAR (-9)0 < n < 4000 1
realSQL_REAL (7)4/4
smalldatetimeSQL_TYPE_TIMESTAMP (93)16/160/0
smallintSQL_SMALLINT (5)2 bytes
SmallmoneySQL_DECIMAL (3)10/104/4
timeSQL_SS_TIME2 (-154)8/160/7
timestampSQL_BINARY (-2)8 bytes
tinyintSQL_TINYINT (-6)1 byte
udtSQL_SS_UDT (-151)variable
uniqueidentifierSQL_GUID (-11)16
varbinarySQL_VARBINARY (-3)0 < n < 8000 1
varcharSQL_VARCHAR (12)0 < n < 8000 1
xmlSQL_SS_XML (-152)0

(1) Zero (0) indicates that the maximum size is allowed.

The Nullable key can either be yes or no.

The following example creates a statement resource, then retrieves and displays the field metadata. 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.

/* 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));  
/* Prepare the statement. */  
$tsql = "SELECT ReviewerName, Comments FROM Production.ProductReview";  
$stmt = sqlsrv_prepare( $conn, $tsql);  
/* Get and display field metadata. */  
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata)  
      foreach( $fieldMetadata as $name => $value)  
           echo "$name: $value\n";  
      echo "\n";  
/* Note: sqlsrv_field_metadata can be called on any statement  
resource, pre- or post-execution. */  
/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  

SQLSRV Driver API Reference
Constants (Microsoft Drivers for PHP for SQL Server)
About Code Examples in the Documentation

Community Additions