Adding SQL Server Access to a LAMP System

This information is specific to Red Hat Enterprise Linux 5 (RHEL) using RPM packages, though you or your system administrator could compile the necessary tools instead.

The idea is to take an existing Linux system with Apache and PHP already installed, and change it to allow database access using ODBC. This requires:

For RHEL, this is relatively easy once you realize that the packages are already built for you.

  1. Install unixODBC
      yum install unixODBC
      yum install unixODBC-devel
      yum install unixODBC-kde
      
  2. Install FreeTDS for SQL Server 2000 and beyond, and using unixODBC
      ./configure --with-tdsver=8.0 --with-unixODBC=/usr/include
      make
      make install
      
  3. Configure /usr/local/etc/freetds.conf:

    The host will be different, and the choice of names in the square brackets is arbitrary, but becomes known as the ServerName.

      [iaidb]
      host = iai-db.insttech.washington.edu
      port = 1433
      tds version = 8.0
      
  4. Configure ODBC via KDE/gnome:
      ODBCConfig
      
  5. Here are the relevant contents of the /etc/odbc.ini file
    [ODBC Data Sources]
    
    [iaidb]
    Description     = FreeTDS
    Driver      = FreeTDS
    Servername      = iaidb
    Database        =
    UID     =
    PWD     =
    Port        = 1433
      
  6. Here are the relevant contents of the /etc/odbcinst.ini file
    [FreeTDS]
    Description = ODBC for SQL Server
    Driver = /usr/local/lib/libtdsodbc.so
    Setup       = /usr/lib/libtdsS.so
    FileUsage       = 1
      
  7. Install the PHP odbc connector and restart httpd:
      yum install php-odbc
      service httpd restart
      
  8. Create this file in a public web space, naming it test_db.php, changing specific information as needed:
      <?php
        $db = odbc_connect("iaidb", "css_test", "password") or die ("couldn't connect");
        $result = odbc_exec($db, "select * from tablename ");
    
        while(odbc_fetch_row($result))
        {
          print(odbc_result($result, "columnname1") . "<br>\n");
        }
    
        odbc_free_result($result);
        odbc_close($db);
      ?>
      
  9. Test from a browser:
      http://cssgate.insttech.washington.edu/~css_test/test_db.php