PDO_DBLIB DSN

(PECL PDO_DBLIB >= 0.9.0)

PDO_DBLIB DSNConnecting to Microsoft SQL Server and Sybase databases

˵Ã÷

The PDO_DBLIB Data Source Name (DSN) is composed of the following elements:

DSN prefix

The DSN prefix is sybase: if PDO_DBLIB was linked against the Sybase ct-lib libraries, mssql: if PDO_DBLIB was linked against the Microsoft SQL Server libraries, or dblib: if PDO_DBLIB was linked against the FreeTDS libraries.

host

The hostname on which the database server resides. Defaults to 127.0.0.1.

dbname

The name of the database.

charset

The client character set.

appname

The application name (used in sysprocesses). Defaults to "PHP Generic DB-lib" or "PHP freetds".

secure

Currently unused.

·¶Àý

Example #1 PDO_DBLIB DSN examples

The following examples show a PDO_DBLIB DSN for connecting to Microsoft SQL Server and Sybase databases:

mssql:host=localhost;dbname=testdb
sybase:host=localhost;dbname=testdb
dblib:host=localhost;dbname=testdb

User Contributed Notes

Anonymous 09-Feb-2018 10:28
Blank (not null) columns retrieved from SQL Server 2014 were returning a single space.

Setting "tds version = 7.0" in /etc/freetds/freetds.conf fixed the issue.
Anonymous 14-Jan-2016 04:57
Instead of specifying tds version and client charset in freetds.conf, you may pass it as a parameter.
<?php $dsn = 'dblib:version=7.0;CHARSET=gb2312;host=domain.example.com;dbname=example;'; ?>
michal at durys dot pl 10-May-2013 08:22
If you're using FreeTDS driver and you want to use "charset" parameter then you may have to edit freetds.conf (e.g. /etc/freetds/freetds.conf) and force connection using at least version 7.0 of the protocol.

tds version = 7.0

Charset parameter accepts all encodings supported by iconv (execute iconv --list to show all encodings).
aero2sing at hotmail dot com 21-Feb-2013 05:26
Be careful if you are using dblib with prepared statement, as it will crash if first execution didn't return anything.

referring to https://bugs.php.net/bug.php?id=40639
ulisse at atc dot bologna dot it 11-Mar-2010 05:38
on Linux ppc64 / PHP 5.1.2 / freetds-0.64 works well this way:

<?php
// ...
$dbh = new PDO("dblib:host=$hostdb;dbname=$dbname", $usr, $psw);
// ...
?>
slach at 74mail dot ru 04-Feb-2009 02:11
when you connect to non standart 1433 port

for win32 use DSN like 'mssql:host=hostname,port;dbname=database'

for FreeTDS and DB-Lib use DSN like  'mssql:host=hostname:port;dbname=database'