Connect to SQL Server through PDO using SQL Server Driver Connect to SQL Server through PDO using SQL Server Driver windows windows

Connect to SQL Server through PDO using SQL Server Driver


Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:

$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");


Mind you that in my experience and also of other (PHP - Why is new SQLSRV driver slower than the old mssql driver?) that using PDO_SQLSRV is way slower than through PDO_ODBC.

If you want to use the faster PDO_ODBC you can use:

//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"$mssqldriver = '{SQL Server}'; $mssqldriver = '{SQL Server Native Client 11.0}';$mssqldriver = '{ODBC Driver 11 for SQL Server}';$hostname='127.0.0.1';$dbname='test';$username='user';$password='pw';$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);


Figured this out. Pretty simple:

 new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]",  "[username]", "[password]");