How to connect to mssql using pdo through PHP and Linux?

richie picture richie · May 10, 2011 · Viewed 77.3k times · Source

I'm trying to for a new PDO connection using the following code.

new PDO("mssql:driver=????;Server={$serverName};Database={$databaseName}", $username, $password, array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

I'm not sure what drivers to use? Or how to install them. I can connect perfectly fine using the mssql_connect function in PHP but I want to use the PDO library instead.

My php.ini settings for mssql are:

ssql

MSSQL Support   enabled
Active Persistent Links     0
Active Links    1
Library version     FreeTDS

Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection Off Off
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

Answer

James picture James · May 11, 2011

The PDO mssql driver is no more, use sqlsrv (under php windows) or dblib (under php linux)

http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

http://www.php.net/manual/en/ref.pdo-dblib.php