Connecting PHP to MS SQLServer 2008 from Linux via PDO

I need to connect to the MS SQLServer 2008 service with PHP on Ubuntu, and I would like to do this using PDO. I believe that I have installed all the necessary libraries, and I can really connect using tsql on the command line and using mssql_connect() in the code. I can’t understand what DSN itself is, or if there are any additional PDO configuration steps that I’m missing.

I use the following DSN (where the $db* variables are populated with the appropriate values):

 odbc:Driver=FreeTDS;SERVER=$dbServer;DATABASE=$dbSchema;UID=$dbUser;PWD=$dbPasswd" 

My error message:

 PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /home/timothy/test.php:4 Stack trace: #0 /home/timothy/test.php(4): PDO->__construct('odbc:Driver=Fre...') #1 {main} thrown in /home/timothy/test.php on line 4 

What additional configuration steps did I see?

Thanks in advance.

+4
source share
1 answer

You do not need additional configuration if you can connect to tsql and the mssql extension. You probably just don't have the correct dsn. This documentation should help. Try new PDO("dblib:host=xx.xx.xx.xx;dbname=mydatabase", "username", "password");

+1
source

All Articles