[freetds] Re: PHP & FreeTDS

Daniel Fazekas fdsubs at axelero.hu
Tue Aug 24 20:22:14 EDT 2004

On Aug 25, 2004, at 1:09, Walls Rob W Contr 75 CS/SCBS wrote:

> I know this is not a PHP group, but I hoping someone knows what to do  
> to
> make this work with PHP without recompiling.

Note that your PHP apparently supports shared extensions, so you could  
add support for the MSSQL extension without recompiling the whole  
thing, if that sounds better than being stuck with ODBC.

> Warning: SQL error: [unixODBC][FreeTDS][SQL Server]Unable to connect  
> to data
> source, SQL state S1000 in SQLConnect in /usr/share/php/common.inc on  
> line
> 25

I hate giving unrelated and unasked for advice, but be very cautious of  
using an extension such as ".inc" for holding php code. That could all  
too easily lead to security problems, unless special precaution is  
taken to configure Apache not to serve a ".inc" file, or to have it run  
through the php interpreter. If such a file ends up in a web-accessible  
directory, people could enter the file's address and since that  
extension does not normally invoke the php interpreter, the full source  
code will be sent to the browser instead.

Now /usr/share/php/ does not sound like a directory anyone would make  
accessible through the web, so most probably there's no risk involved  
in this case. Sorry if you were aware of this, I just felt it was worth  
mentioning. Generally it's better to use extensions like ".inc.php"  

> I'm using this code to connect:
> $connection_string =
> $DB;';
> $connection = odbc_connect( $connection_string, $DB_LOGIN,  

Would it be unfeasible for your project to move most of the connection  
parameters into the separate configuration files? I'm not really  
familiar with these jumbo ODBC connection strings. ;)

However, note that since you are setting $connection_string with single  
apostrophes, the $variables in the string are not getting expanded and  
are kept without modification. That is, it remains UID=$DB_LOGIN, and  
won't get changed to UID=whatever_is_in_$DB_LOGIN, which is probably  
what you wanted.
You'd have to quote the string with " for that expansion to take place.  
This is the big difference between ' and " in php. I can't guarantee  
this small change will make it all work, though. :)

The following works for me:

In /usr/local/etc/freetds.conf:

tds version=8.0
client charset = utf-8

In /etc/odbcinst.ini:

Description = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so

In /etc/odbc.ini:

Driver = TDS
Servername = TheNameSpecifiedInFreetdsConf

And in the PHP code you just initialize your connection as

$conn = odbc_connect('TheOdbcIniDataSourceName', 'username',  

[When using mssql_connect(), you would have to specify  
'TheNameSpecifiedInFreetdsConf' as the server name.]

> PHP finds an odbc.ini file in /etc/php.d/ which loads  
> /usr/lib/php4/odbc.so

That sounds odd. PHP does not look for or use a file called odbc.ini in  
order to load its shared extensions. odbc.so is probably loaded from  
your php.ini instead, which is in /etc/php.ini on Red Hat by default.


More information about the FreeTDS mailing list