0

First of all I know there are many questions posted for the same issue. I have tried every thing but couldn't get it to work. I am trying to connect to SQL Server using PHP. I've configured FreeTDS and unixODBC correctly. I tried tsql, isql and telnet and it connects to the server without an issue. But when I try to connect using PHP and execute the script in browser, it gives me this error:

SQLSTATE[08S01] SQLDriverConnect: 20009 [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist

PHP Script

<?php

putenv('ODBCSYSINI=/etc'); 
putenv('ODBCINI=/etc/odbc.ini');
try { 
  $dbh = new PDO('odbc:U18958173','<USER_NAME>','<PASSWORD>'); 
} catch (PDOException $exception) { 
  echo $exception->getMessage(); 
  exit; 
} 
echo var_dump($dbh); 
unset($dbh); 

freetds.conf

[U18958173]
host = <SERVER_IP>
instance = SQLEXPRESS
#port = 1433
tds version = auto
client charset = UTF-8

odbc.ini

[U18958173]
Driver = FreeTDS
Description = MSSQL database for my nice app
Servername = U18958173
UserName = <USER_NAME>
Password = <PASSWORD>
Database = <DATABASE>
TDS_VERSION = auto

When I browse my PHP script in browser it gives me the error I mentioned above. Please help me, I've been searching for the solution for the last 3 days. Thank you.

  • Have you checked that your environment variables are being picked up by PHP? – Jonathan Mar 19 '16 at 20:36
  • You're setting some environment variables but what about the rest? How are they being loaded into the environment? What happens when you try connecting with the connection details explicitly defined? Use `getenv()` to load a variable from the environment. – Jonathan Mar 19 '16 at 20:43
  • Hi, can you please explain me more, actually I copied that piece of code from somewhere. Thanks – Muzammil Versiani Mar 19 '16 at 20:58
  • Okay. I used `echo getenv('ODBCINI')` and it printed the same value `/etc/odbc.ini` – Muzammil Versiani Mar 19 '16 at 21:15
  • So where you are putting ``, you are putting the actual username, and so on for the others? Did you try setting the credentials like this to see if it is your environment which is at fault? – Jonathan Mar 20 '16 at 14:24

0 Answers0