Share via


Default PHP Data Types

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 Type

Default PHP Type

Default Encoding

bigint

String

8-bit character1

binary

Stream2

Binary3

bit

Integer

8-bit character1

char

String

8-bit character1

date8

Datetime

Not applicable

datetime8

Datetime

Not applicable

datetime28

Datetime

Not applicable

datetimeoffset8

Datetime

Not applicable

decimal

String

8-bit character1

float

Float

8-bit character1

geography

STREAM

Binary3

geometry

STREAM

Binary3

image4

Stream2

Binary3

int

Integer

8-bit character1

money

String

8-bit character1

nchar

String

8-bit character1

numeric

String

8-bit character1

nvarchar

String

8-bit character1

nvarchar(MAX)

Stream2

8-bit character1

ntext5

Stream2

8-bit character1

real

Float

8-bit character1

smalldatetime

Datetime

8-bit character1

smallint

Integer

8-bit character1

smallmoney

String

8-bit character1

sql_variant

String

8-bit character1

text6

Stream2

8-bit character1

time8

Datetime

Not applicable

timestamp

String

8-bit character1

tinyint

Integer

8-bit character1

UDT

Stream2

Binary3

uniqueidentifier

String7

8-bit character1

varbinary

Stream2

Binary3

varbinary(MAX)

Stream2

Binary3

varchar

String

8-bit character1

varchar(MAX)

Stream2

8-bit character1

variant

Not supported

Not supported

xml

Stream2

8-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.

Other New SQL Server 2008 Data Types and Features

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.

Feature

PHP Support

Table-valued parameter

No

Sparse columns

Partial

Null-bit compression

Yes

Large CLR user-defined types (UDTs)

Yes

Service principal name

No

MERGE

Yes

FILESTREAM

Partial

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

See Also

Reference

sqlsrv_field_metadata

Concepts

Constants (Microsoft Drivers for PHP for SQL Server)

Other Resources

Converting Data Types

PHP Types

Data Types (Transact-SQL)