Timothy
Timothy

Reputation: 4650

PHP Connection to MS SQLServer 2008 from Linux via PDO

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):

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

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.

Upvotes: 4

Views: 5311

Answers (1)

Lucas
Lucas

Reputation: 110

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

Upvotes: 1

Related Questions