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

后端 未结 3 1728
旧巷少年郎
旧巷少年郎 2020-12-15 20:04

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 p

3条回答
  •  -上瘾入骨i
    2020-12-15 20:50

    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.

提交回复
热议问题