php

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

nauleyco

Noriko Yamamoto

Posted on June 17, 2020

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
đź’– đź’Ş đź™… đźš©
nauleyco
Noriko Yamamoto

Posted on June 17, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related