Constants (Microsoft Drivers for PHP for SQL Server)

 

DownloadDownload PHP Driver

This topic discusses the constants that are defined by the Microsoft Drivers for PHP for SQL Server.

The constants listed on the PDO website are valid in the Microsoft Drivers for PHP for SQL Server.

The following describe the Microsoft specific constants in the PDO_SQLSRV driver.

Transaction Isolation Level Constants

The TransactionIsolation key, which is used with PDO::__construct, accepts one of the following constants:

  • PDO::SQLSRV_TXN_READ_UNCOMMITTED

  • PDO::SQLSRV_TXN_READ_COMMITTED

  • PDO::SQLSRV_TXN_REPEATABLE_READ

  • PDO::SQLSRV_TXN_SNAPSHOT

  • PDO::SQLSRV_TXN_SERIALIZABLE

For more information about the TransactionIsolation key, see Connection Options.

Encoding Constants

The PDO::SQLSRV_ATTR_ENCODING attribute can be passed to PDOStatement::setAttribute, PDO::setAttribute, PDO::prepare, PDOStatement::bindColumn, and PDOStatement::bindParam.

The available values to pass to PDO::SQLSRV_ATTR_ENCODING are

PDO_SQLSRV driver constantDescription
PDO::SQLSRV_ENCODING_BINARYData is a raw byte stream from the server without performing encoding or translation.

Not valid for PDO::setAttribute.
PDO::SQLSRV_ENCODING_SYSTEMData is 8-bit characters as specified in the code page of the Windows locale that is 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.
PDO::SQLSRV_ENCODING_UTF8Data is in the UTF-8 encoding. This is the default encoding.
PDO::SQLSRV_ENCODING_DEFAULTUses PDO::SQLSRV_ENCODING_SYSTEM if specified during connection.

Use the connection’s encoding if specified in a prepare statement.

Query Timeout

The PDO::SQLSRV_ATTR_QUERY_TIMEOUT attribute is any non-negative integer representing the timeout period, in seconds. Zero (0) is the default and means no timeout.

You can specify the PDO::SQLSRV_ATTR_QUERY_TIMEOUT attribute with PDOStatement::setAttribute, PDO::setAttribute, and PDO::prepare.

Direct or Prepared Execution

You can select direct query execution or prepared statement execution with the PDO::SQLSRV_ATTR_DIRECT_QUERY attribute. PDO::SQLSRV_ATTR_DIRECT_QUERY can be set with PDO::prepare or PDO::setAttribute. For more information about PDO::SQLSRV_ATTR_DIRECT_QUERY, see Direct Statement Execution and Prepared Statement Execution in the PDO_SQLSRV Driver.

The following sections list the constants used by the SQLSRV driver.

ERR Constants

The following table lists the constants that are used to specify if sqlsrv_errors returns errors, warnings, or both.

ValueDescription
SQLSRV_ERR_ALLErrors and warnings generated on the last sqlsrv function call are returned. This is the default value.
SQLSRV_ERR_ERRORSErrors generated on the last sqlsrv function call are returned.
SQLSRV_ERR_WARNINGSWarnings generated on the last sqlsrv function call are returned.

FETCH Constants

The following table lists the constants that are used to specify the type of array returned by sqlsrv_fetch_array.

SQLSRV constantDescription
SQLSRV_FETCH_ASSOCsqlsrv_fetch_array returns the next row of data as an associative array.
SQLSRV_FETCH_BOTHsqlsrv_fetch_array returns the next row of data as an array with both numeric and associative keys. This is the default value.
SQLSRV_FETCH_NUMERICsqlsrv_fetch_array returns the next row of data as a numerically indexed array.

Logging Constants

This section lists the constants that are used to change the logging settings with sqlsrv_configure. For more information about logging activity, see Logging Activity.

The following table lists the constants that can be used as the value for the LogSubsystems setting:

SQLSRV constant (integer equivalent in paraentheses)Description
SQLSRV_LOG_SYSTEM_ALL (-1)Turns on logging of all subsystems.
SQLSRV_LOG_SYSTEM_CONN (2)Turns on logging of connection activity.
SQLSRV_LOG_SYSTEM_INIT (1)Turns on logging of initialization activity.
SQLSRV_LOG_SYSTEM_OFF (0)Turns logging off.
SQLSRV_LOG_SYSTEM_STMT (4)Turns on logging of statement activity.
SQLSRV_LOG_SYSTEM_UTIL (8)Turns on logging of error functions activity (such as handle_error and handle_warning).

The following table lists the constants that can be used as the value for the LogSeverity setting:

SQLSRV constant (integer equivalent in paraentheses)Description
SQLSRV_LOG_SEVERITY_ALL (-1)Specifies that errors, warnings, and notices will be logged.
SQLSRV_LOG_SEVERITY_ERROR (1)Specifies that errors will be logged.
SQLSRV_LOG_SEVERITY_NOTICE (4)Specifies that notices will be logged.
SQLSRV_LOG_SEVERITY_WARNING (2)Specifies that warnings will be logged.

Nullable Constants

The following table lists the constants that you can use to determine whether or not a column is nullable or if this information is not available. You can compare the value of the Nullable key that is returned by sqlsrv_field_metadata to determine the column's nullable status.

SQLSRV constant (integer equivalent in paraentheses)Description
SQLSRV_NULLABLE_YES (0)The column is nullable.
SQLSRV_NULLABLE_NO (1)The column is not nullable.
SQLSRV_NULLABLE_UNKNOWN (2)It is not known if the column is nullable.

PARAM Constants

The following list contains the constants for specifying parameter direction when you call sqlsrv_query or sqlsrv_prepare.

SQLSRV constantDescription
SQLSRV_PARAM_INIndicates an input parameter.
SQLSRV_PARAM_INOUTIndicates a bidirectional parameter.
SQLSRV_PARAM_OUTIndicates an output parameter.

PHPTYPE Constants

The following table lists the constants that are used to describe PHP data types. For information about PHP data types, see PHP Types.

SQLSRV constantPHP data type
SQLSRV_PHPTYPE_INTInteger
SQLSRV_PHPTYPE_DATETIMEDatetime
SQLSRV_PHPTYPE_FLOATFloat
SQLSRV_PHPTYPE_STREAM(1)Stream
SQLSRV_PHPTYPE_STRING(1)String
  1. SQLSRV_PHPTYPE_STREAM and SQLSRV_PHPTYPE_STRING accept a parameter that specifies the stream encoding. The following table contains the SQLSRV constants that are acceptable parameters, and a description of the corresponding encoding.
SQLSRV constantDescription
SQLSRV_ENC_BINARYData is returned as a raw byte stream from the server without performing encoding or translation.
SQLSRV_ENC_CHARData is returned in 8-bit characters as specified in the code page of the Windows locale that is 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.

This is the default encoding.
"UTF-8"Data is returned in the UTF-8 encoding. This constant was added in version 1.1 of the Microsoft Drivers for PHP for SQL Server. For more information about UTF-8 support, see How to: Send and Retrieve UTF-8 Data Using Built-In UTF-8 Support.
System_CAPS_ICON_note.jpg Note


When you use SQLSRV_PHPTYPE_STREAM or SQLSRV_PHPTYPE_STRING, the encoding must be specified. If no parameter is supplied, an error will be returned.

For more information about these constants, see How to: Specify PHP Data Types, How to: Retrieve Character Data as a Stream Using the SQLSRV Driver.

SQLTYPE Constants

The following table lists the constants that are used to describe SQL Server data types. Some constants require parameters that correspond to precision, scale, and/or length. For information about SQL Server data types, see Data Types (Transact-SQL). For information about precision, scale, and length, see Precision, Scale, and Length (Transact-SQL).

SQLSRV constantSQL Server data type
SQLSRV_SQLTYPE_BIGINTbigint
SQLSRV_SQLTYPE_BINARYbinary
SQLSRV_SQLTYPE_BITbit
SQLSRV_SQLTYPE_CHAR($charCount)char
SQLSRV_SQLTYPE_DATEdate4
SQLSRV_SQLTYPE_DATETIMEdatetime
SQLSRV_SQLTYPE_DATETIME2datetime24
SQLSRV_SQLTYPE_DATETIMEOFFSETdatetimeoffset4
SQLSRV_SQLTYPE_DECIMAL($precision, $scale)decimal
SQLSRV_SQLTYPE_FLOATfloat
SQLSRV_SQLTYPE_IMAGEimage1
SQLSRV_SQLTYPE_INTint
SQLSRV_SQLTYPE_MONEYmoney
SQLSRV_SQLTYPE_NCHAR($charCount)nchar
SQLSRV_SQLTYPE_NUMERIC($precision, $scale)numeric
SQLSRV_SQLTYPE_NVARCHAR($charCount)nvarchar
SQLSRV_SQLTYPE_NVARCHAR('max')nvarchar(MAX)
SQLSRV_SQLTYPE_NTEXTntext2
SQLSRV_SQLTYPE_REALreal
SQLSRV_SQLTYPE_SMALLDATETIMEsmalldatetime
SQLSRV_SQLTYPE_SMALLINTsmallint
SQLSRV_SQLTYPE_SMALLMONEYsmallmoney
SQLSRV_SQLTYPE_TEXTtext3
SQLSRV_SQLTYPE_TIMEtime4
SQLSRV_SQLTYPE_TIMESTAMPtimestamp
SQLSRV_SQLTYPE_TINYINTtinyint
SQLSRV_SQLTYPE_UNIQUEIDENTIFIERuniqueidentifier
SQLSRV_SQLTYPE_UDTUDT
SQLSRV_SQLTYPE_VARBINARY($byteCount)varbinary
SQLSRV_SQLTYPE_VARBINARY('max')varbinary(MAX)
SQLSRV_SQLTYPE_VARCHAR($charCount)varchar
SQLSRV_SQLTYPE_VARCHAR('max')varchar(MAX)
SQLSRV_SQLTYPE_XMLxml
  1. This is a legacy type that maps to the varbinary(max) type.

  2. This is a legacy type that maps to the newer nvarchar type.

  3. This is a legacy type that maps to the newer varchar type.

  4. Support for this type was added in version 1.1 of the Microsoft Drivers for PHP for SQL Server.

The following table lists the SQLTYPE constants that accept parameters and the range of values allowed for the parameter.

SQLTYPEParameterAllowable range for parameter
SQLSRV_SQLTYPE_CHAR,

SQLSRV_SQLTYPE_VARCHAR
charCount1 - 8000
SQLSRV_SQLTYPE_NCHAR,

SQLSRV_SQLTYPE_NVARCHAR
charCount1 - 4000
SQLSRV_SQLTYPE_BINARY,

SQLSRV_SQLTYPE_VARBINARY
byteCount1 - 8000
SQLSRV_SQLTYPE_DECIMAL,

SQLSRV_SQLTYPE_NUMERIC
precision1 - 38
SQLSRV_SQLTYPE_DECIMAL,

SQLSRV_SQLTYPE_NUMERIC
scale1 - precision

Transaction Isolation Level Constants

The TransactionIsolation key, which is used with sqlsrv_connect, accepts one of the following constants:

  • SQLSRV_TXN_READ_UNCOMMITTED

  • SQLSRV_TXN_READ_COMMITTED

  • SQLSRV_TXN_REPEATABLE_READ

  • SQLSRV_TXN_SNAPSHOT

  • SQLSRV_TXN_SERIALIZABLE

Cursor and Scrolling Constants

The following constants specify the kind of cursor that you can use in a result set:

  • SQLSRV_CURSOR_FORWARD

  • SQLSRV_CURSOR_STATIC

  • SQLSRV_CURSOR_DYNAMIC

  • SQLSRV_CURSOR_KEYSET

  • SQLSRV_CURSOR_CLIENT_BUFFERED

The following constants specify which row to select in the result set:

  • SQLSRV_SCROLL_NEXT

  • SQLSRV_SCROLL_PRIOR

  • SQLSRV_SCROLL_FIRST

  • SQLSRV_SCROLL_LAST

  • SQLSRV_SCROLL_ABSOLUTE

  • SQLSRV_SCROLL_RELATIVE

For information on using these constants, see Specifying a Cursor Type and Selecting Rows.

SQLSRV Driver API Reference

Community Additions

ADD
Show: