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.
Yes, Microsoft provides a MS SQL driver for PHP.
The SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php. ini file. The SQLSRV download comes with 8 driver files, four of which are for PDO support. The most recent version of the driver is available for download here: » SQLSRV download.
Microsoft ODBC Driver for SQL Server is a single dynamic-link library (DLL) containing run-time support for applications using native-code APIs to connect to SQL Server.
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.
PDO allows you to write you code to be reasonably DB-neutral.
If you want truly DB-neutral, you'd want to use a full DB abstraction layer like NotORM -- with plain PDO, you'd still need to be careful about SQL syntax difference, but at least your basic PHP code would be DB-neutral.
Being DB-neutral may not seem important now -- if you're using SQL Server, then you've probably been told that's what is required and nothing else -- but you can't predict how things will change in the future, so if the choice is between a DB-neutral driver and a DB-specific driver, and you don't have any other reason for a preference, then go with the neutral one.... it'll make life a lot easier if your company gets taken over and the new boss wants to use Oracle as the DB!
Also, because it's DB neutral, PDO is more standard and more well-known in the PHP community. You'll get a lot more help with PDO from sites online (like this one) than with the MSSQL driver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With