I tested under Fedora 15 and CentOS 5.4 and it works fine, using FreeTDS!

Easy steps:

1. Install unixODBC and freetds:

yum install freetds unixODBC

2. Configure unixODBC to use FreeTDS adding the following code to your /etc/odbcinst.ini file (check the red lines, because the files can change):

[FreeTDS]
Description        = ODBC for FreeTDS
Driver        = /usr/lib/libtdsodbc.so
Setup        = /usr/lib/libtdsS.so.2
Driver64        = /usr/lib64/libtdsodbc.so
Setup64        = /usr/lib64/libtdsS.so.2
FileUsage        = 1
UsageCount        = 1

[ODBC Drivers]
FreeTDS = Installed

3. Create your DSN into /etc/odbc.ini file:

[connection_name]
Driver = /usr/lib64/libtdsodbc.so
Description = My Connection Name
Trace = No
Server = 192.168.0.3
Port = 1433
UID = sql_username_here
Password = sql_password_here
Database = DATABASE_NAME_HERE

[ODBC Data Sources]
connection_name = FreeTDS

4. Test your configuration:

isql -v connection_name sql_username_here sql_password_here

The result will be something like:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

5. Install PHP-ODBC:

yum install php-5.2-odbc-zend-server.x86_64

6. Finally create your PHP Script:

<?php

putenv ( 'ODBCINI=/etc/odbc.ini' );
putenv ( "ODBCINSTINI=/etc/odbcinst.ini" );

$conn = odbc_connect ( 'connection_name', 'sql_username_here', 'sql_password_here' ) or die ( 'Error: ' . odbc_error () );
if (! $conn) {
    die ( 'Connection error' );
}

$sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
$res = odbc_exec ( $conn, $sql );
odbc_result_all ( $res );

odbc_close ( $conn );

Easy!

Facebooktwitterredditpinterestlinkedinmail