Search code examples
phpsql-serverwindowspdo

Connect to SQL Server through PDO using SQL Server Driver


I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft.

I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?

Are there any good examples of how to properly do this? If I should be doing this via some other method please let me know. Thanks!


Solution

  • 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");