Unicode Support and Endianness

We’ve seen more activity on our MSDN Forum over the past couple weeks (yay!) and there have been a few threads about how/if the SQL Server Driver for PHP supports Unicode.  Rest assured, the SQL Server Driver for PHP does support Unicode.  The driver expects Unicode strings to be in little endian format, since that's what SQL Server uses.


When you convert strings between different encodings in PHP using iconv or mb_convert_encoding, PHP will assume big endian format if you do not specify endianness.  In other words, you must specify "ucs-2le" instead of just "ucs-2".  The example in our documentation for working with UTF-8 strings did not specify endianness so the strings were converted to and from Unicode in big endian format.  We're in the process of correcting that error and apologize for the confusion.


For more information on endianness and the origin of the term "endian", see Michael Kaplan's blog post or the Wikipedia entry on endianness.


David Sceppa

Program Manager - Microsoft SQL Server Driver for PHP

Comments (3)

  1. DreamDevil says:

    I doing a little test drive with this driver.  In the line of "automating" data conversion from/to UTF-8, I am using sqlsrv_field_metadata.   Depending on the datatype, I want to get the field value (using SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)) and iconv).

    My problem comes when I use the constants to compare with numeric value of "type" in the metadata array.  A var_dump of the metadata for a field of type NVARCHAR(255) gives this:


         ‘Name’ => string ‘aliasName’ (length=9)

         ‘Type’ => int -9

         ‘Size’ => int 255

         ‘Precision’ => null

         ‘Scale’ => null

         ‘Nullable’ => int 1

    The documentation says the "Type" is an SQL Type and if I use SQLSRV_SQLTYPE_NVARCHAR:

    1- I am forced to enter a size while I just want to know if it is a NVARCHAR no matter its size.

    2- SQLSRV_SQLTYPE_NVARCHAR(255) returns this: 2139226103

    Note: if I specify a different size, I get a different value.

    Note2: No matter the size of the field the metadata will always return -9 for a NVARCHAR field (makes sense)

    I understand the purpose of those constants for defining query parameters, but they cannot be used against the Type value of the metadata.

    Is my only option is to hardcode numerical values?


  2. DreamDevil says:

    In the driver documentation, all the sample I see for UTF-8 support uses parameters.

    Is is possible to run a query that has hardcoded where clause?


    $tsql = "SELECT * FROM MyTable WHERE name LIKE ‘%é%’"

    $stmt = sqlsrv_query($conn, $tsql);


Skip to main content