Default PHP Data Types

 

DownloadDownload PHP Driver

When retrieving data from the server, the Microsoft Drivers for PHP for SQL Server converts data to a default PHP data type if no PHP data type has been specified by the user.

When data is returned using the PDO_SQLSRV driver, the data type will either be int or string.

The remainder of this topic discusses default data types using the SQLSRV driver.

The following table lists the SQL Server data type (the data type being retrieved from the server), the default PHP data type (the data type to which data is converted), and the default encoding for streams and strings. For details about how to specify data types when retrieving data from the server, see How to: Specify PHP Data Types.

SQL Server TypeDefault PHP TypeDefault Encoding
bigintString8-bit character1
binaryStream2Binary3
bitInteger8-bit character1
charString8-bit character1
date8DatetimeNot applicable
datetime8DatetimeNot applicable
datetime28DatetimeNot applicable
datetimeoffset8DatetimeNot applicable
decimalString8-bit character1
floatFloat8-bit character1
geographySTREAMBinary3
geometrySTREAMBinary3
image4Stream2Binary3
intInteger8-bit character1
moneyString8-bit character1
ncharString8-bit character1
numericString8-bit character1
nvarcharString8-bit character1
nvarchar(MAX)Stream28-bit character1
ntext5Stream28-bit character1
realFloat8-bit character1
smalldatetimeDatetime8-bit character1
smallintInteger8-bit character1
smallmoneyString8-bit character1
sql_variantString8-bit character1
text6Stream28-bit character1
time8DatetimeNot applicable
timestampString8-bit character1
tinyintInteger8-bit character1
UDTStream2Binary3
uniqueidentifierString78-bit character1
varbinaryStream2Binary3
varbinary(MAX)Stream2Binary3
varcharString8-bit character1
varchar(MAX)Stream28-bit character1
variantNot supportedNot supported
xmlStream28-bit character1
  1. Data is returned in 8-bit characters as specified in the code page of the Windows locale set on the system. Any multi-byte characters or characters that do not map into this code page are substituted with a single byte question mark (?) character.

  2. If sqlsrv_fetch_array or sqlsrv_fetch_object is used to retrieve data that has a default PHP type of Stream, the data will be returned as a string with the same encoding as the stream. For example, if a SQL Server binary type is retrieved by using sqlsrv_fetch_array, the default return type will be a binary string.

  3. Data is returned as a raw byte stream from the server without performing encoding or translation.

  4. This is a legacy type that maps to the varbinary(max) type.

  5. This is a legacy type that maps to the nvarchar(max) type.

  6. This is a legacy type that maps to the varchar(max) type.

  7. UNIQUEIDENTIFIERs are GUIDs represented by the following regular expression:

    [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-f]{4}-[0-9a-fA-f]{4}-[0-9a-fA-F]{12}

  8. Date and time types can be retrieved as strings. For more information, see How to: Retrieve Date and Time Type as Strings Using the SQLSRV Driver.

Data types that are new in SQL Server 2008 and that exist outside of columns (such as table-valued parameters) are not supported in the Microsoft Drivers for PHP for SQL Server. The table below summarizes the PHP support for new SQL Server 2008 features.

FeaturePHP Support
Table-valued parameterNo
Sparse columnsPartial
Null-bit compressionYes
Large CLR user-defined types (UDTs)Yes
Service principal nameNo
MERGEYes
FILESTREAMPartial

Partial type support means that you cannot programmatically query for the type of the column.

Constants (Microsoft Drivers for PHP for SQL Server)
Converting Data Types
PHP Types
Data Types (Transact-SQL)
sqlsrv_field_metadata

Community Additions

ADD
Show: