DEV Community

Noriko Yamamoto
Noriko Yamamoto

Posted on

How to connect to MS SQL Server 2000 in PHP7 PDO on CentOS 7

As a premise, php and pdo are installed.

At first, install nixODBC-devel and freetds and php-odbc.

$ sudo yum -y install unixODBC-devel
$ sudo yum -y install freetds
$ sudo yum -y install php-odbc
Enter fullscreen mode Exit fullscreen mode

And then, edit /etc/odbcinst.ini

$ sudo vi /etc/odbcinst.ini
Enter fullscreen mode Exit fullscreen mode

[/etc/odbcinst.ini]:

[FreeTDS]
Description = ODBC for FreeTDS
Driver64    = /usr/lib64/libtdsodbc.so
Setup64     = /usr/lib64/libtdsodbc.so
FileUsage   = 1
Enter fullscreen mode Exit fullscreen mode

Finally, create an PDO object as follows.

<?php
    $host = 'xxx.xxx.xxx.xxx';
    $port = '1433';
    $dbname = 'database-name';
    $username = 'username';
    $password = 'password';

    $dsn = "odbc:Driver=FreeTDS;" . 
        "SERVER={$host};PORT={$port};DATABASE={$dbname};" . 
        "ClientCharset=UTF-8";
    $pdo = new \PDO($dsn, $username, $password);
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tomtomklima profile image
Tom K

Hi! Did you try connect to MSSQL 2000 in PHP7 on Ubuntu?