Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified

peird picture peird · Dec 23, 2013 · Viewed 15k times · Source

I'm stuck with this problem for a while and I just can't get any further, I did a lot of searches but nothing works... I'm trying to connect to a Microsoft SQL Database with php using odbc.

Everything is set up, as follows (the values between "" are correct in the file):

/etc/odbc.ini:

[CRMCONNECT]
Description = "CRMConnect"
Driver = FreeTDS
Trace = No
Servername = CRMSERVER
Database = "dbname"
UserName = "username"
Password = "password"

[Default]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

/etc/odbcinst.ini:

[FreeTDS]
Description = tdsodbc
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout       = 5
CPReuse         = 5
FileUsage       = 1

/etc/freetds/freetds.conf:

[CRMSERVER]
host = xxx.xxx.xxx.xxx 
port = 1433
tds version = 8.0

I doublechecked the host many times and it is correct. I also tried tds version 7.0, but no luck.

I can succesfully connect to the server with isql:

root@crmart-web004:/# isql -v CRMCONNECT "user" "pass"
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

But with php I just can't get it working, I get the following error:

[unixODBC][Driver Manager]Data source name not found, and no default driver specified 

My connectionstring:

$connection = odbc_connect("Driver={CRMCONNECT};Server=xxx.xxx.xxx.xxx;Database=dbname;","username","password");

All parameters are doublechecked and are correct.

How come I can successfully connect with isql but it fails in php?

My php version:

PHP Version 5.4.4-14+deb7u5

odbcinst configuration:

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

I hope someone has an idea what could be wrong.

Thanks in advance

Regards

UPDATE:

I changed my connectionstring to:

$connection = odbc_connect("CRMCONNECT;Database=dbname;","user","pass");

which is resulting in another error:

[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed

I'll be looking into that, thanks again vinodadhikary

Regards.

UPDATE 2:

My connection string was wrong, it should have been:

$connection = odbc_connect("CRMCONNECT","user","pass");

Thanks vinodadhikary!

Regards and happy holidays.

Answer

vee picture vee · Dec 23, 2013

Since you already have CRMCONNECT DSN defined, you could use the following connection method:

$connection = odbc_connect("CRMCONNECT","username","password");

Also in your connection string you have Driver={CRMCONNECT};. CRMCONNECT as you've defined is not a driver, it is a Data Source Name. The driver in your case would be FreeTDS