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

StackOverflow https://stackoverflow.com/questions/20739588

  •  20-09-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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

OTHER TIPS

I had the same error in Laravel 5.5 with PHP 7.1.9 on Debian 7.11. Fixed it by removing curly braces from driver's name in DSN:

Failed: "odbc:Driver={fail};Server=host;Database=db;"

Worked: "odbc:Driver=success;Server=host;Database=db;"

Another working option was moving data source configuration from this string into odbc.ini and then referencing it: "odbc:odbc_ini_data_source_name"

The funny thing is that the same data source with Driver={SQL Server} (with curly braces) worked correctly under Windows 10.

The same applies for PDO. $conn = new PDO("odbc:CRMCONNECT"); and be sure to set your default driver in odbc.ini

[Default] Driver = IBM i Access ODBC Driver 64-bit in our case

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top