SQLSRV driver vs. PDO driver for PHP with MS SQL Server

nmc picture nmc · Jul 23, 2012 · Viewed 18.7k times · Source

What considerations should I take into account when choosing between SQLSRV driver vs. PDO driver (for PHP with MS SQL server)?

I saw this previous Stackoverflow post ( When using PHP on Windows, what is better (1) the native driver for SQL Server or (2) the PDO driver? ) but the answer seems a bit lacking and doesn't mention all the benefits of using the SQLSRV driver as mentioned in this article.

I'm looking for a comprehensive and up-to-date (eg. is it still the case that SQLSRV driver is only available for Windows?) answer that programmers can refer to as a resource.

Answer

Robert Calhoun picture Robert Calhoun · Dec 7, 2012

SQLSRV and PDO_SQLSRV are the two current-generation php drivers available from Microsoft, but both use the same code underneath: SQL Server Native Client 11. (That's why there's no Mac or Linux version of the php drivers: they are just wrappers.) Performance of the two drivers should be similar; it's just a matter of which API you prefer.

In most cases one would use the PDO_SQLSRV driver because of cross-platform considerations. However, after looking at both drivers for a new (small) project I went with the SQLSRV driver because it returns data as [a map of] the underlying SQL Server datatypes, whereas the PDO_SQLSRV returns everything as a string.

So if your sql is:

SELECT 1234 as integer, Cast(123.456 as float) as float, 
       getdate() as date, '1234' as string1,'123.456' as string2;

Then var_dump of the row from PDO_SQLSRV gives:

  array(1) {
    [0] =>
    array(5) {
      'integer' =>
      string(4) "1234"
      'float' =>
      string(7) "123.456"
      'date' =>
      string(23) "2012-12-06 22:35:05.373"
      'string1' =>
      string(4) "1234"
      'string2' =>
      string(7) "123.456"
    }
  }

while the SQLSRV driver gives:

array(1) {
    [0] =>
    array(5) {
      'integer' =>
      int(1234)
      'float' =>
      double(123.456)
      'date' =>
      class DateTime#1 (3) {
        ...
      }
      'string1' =>
      string(4) "1234"
      'string2' =>
      string(7) "123.456"
    }
  }

It drove me nuts that PDO_SQLSRV cast all of my data to a string whether I wanted it to or not, so I used SQLSRV. (I have to admit I set ReturnDatesAsStrings=true because I was too lazy to deal with the date class.)

I also like the syntax a bit better, but that's just me.