27

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
double-beep
  • 5,031
  • 17
  • 33
  • 41
richie
  • 781
  • 2
  • 11
  • 24

4 Answers4

31

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

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
James
  • 20,957
  • 5
  • 26
  • 41
  • using dblib still produces the missing drivers error. "Uncaught exception 'PDOException' with message 'could not find driver'". I find it strange that mssql_connect works fine yet I have to do something in order to get the PDO side working. I'm not very familiar with linux which means that I'm unsure of getting the drivers that I need sorted out? – richie May 12 '11 at 15:05
  • 2
    You need to enable pdo_dblib.so in your php.ini and set up FreeTDS under Linux. – James May 13 '11 at 17:21
  • how do you enable pdo_dblib in configure? also I am using centos 5 32bit, how would I set it up? – shorif2000 Mar 01 '13 at 14:09
  • pdo_dblib no longer works with PHP 5.4+... I can't find a suitable solution to this problem. – Dominick Mar 24 '14 at 20:48
  • @James Is the PHP [Microsoft PDO_SQLSRV](http://php.net/manual/en/ref.pdo-sqlsrv.php) deprecated or is there no problems using it? – Honinbo Shusaku Aug 24 '15 at 14:18
  • @Abdul Not that I'm aware of, why do you ask? – James Aug 24 '15 at 15:09
  • @James I misunderstood your answer and thought that you can't use PDOs with mssql, but I'm guessing that's the `PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");` one that you can't use, and I need to import the MS drivers – Honinbo Shusaku Aug 24 '15 at 15:15
  • Correct, "mssql" stopped being provided with PHP in 5.3 and "sqlsrv" and "dblib" are the current drivers (to my knowledge, it's been a while) – James Aug 24 '15 at 16:56
5

I am running Ubuntu 14.04. Trying to connect to MSSQL I got "Uncaught exception 'PDOException' with message 'could not find driver'". It seems that I was missing the dblib/sybase PHP extension.

I had to run:

sudo apt-get install php5-sybase freetds-common libsybdb5 \ 
&& sudo apache2ctl restart

Works fine now.

Karl Wilbur
  • 5,898
  • 3
  • 44
  • 54
1

Try

$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

$hostname may need to be configured as either...
$hostname.':'.$port;

OR

$hostname.','.$port;
csi
  • 9,018
  • 8
  • 61
  • 81
  • 2
    The error message that I get is Uncaught exception 'PDOException' with message 'could not find driver' – richie May 11 '11 at 09:35
0

The dblib/sybase PHP extension tip posted by Karl Wilbur worked for me. The pre-installation check page for LimeSurvey now shows

PHP PDO driver library- Microsoft SQL Server (dblib), MySQL

Just make sure you find and install the version that aligns with the PHP version you are using;

Prompt>sudo apt-get install php<appropriate version>-sybase freetds-common libsybdb5 Prompt>sudo apache2ctl restart

Cheers,