I need to connect to a MS SQLServer 2008 service from PHP on Ubuntu, and I would like to do so using PDO. I believe I have installed all the prerequisite libraries, and I am indeed able to connect using tsql on the command line and using mssql_connect() in code. I can't figure out what the proper DSN is, or if there are any additional PDO-specific configuration steps I am missing.

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


My error message is:

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 have I over looked?

Thanks in advance.


In your php.ini what is this set to? 'mssql.secure_connection' if off turn on and retest. make sure to restart Apache as well

Written by Phill Pafford

mssql.secure_connection is off. But keep in mind mssql_* functions work, I'm just trying to use the PDO interface instead.

Written by Timothy

Accepted Answer

You shouldn't need any additional configuration if you can connect with 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");

Written by Lucas
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki