Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 596
peird Avatar asked Dec 23 '13 08:12

peird


People also ask

How do I fix Error Data Source Name not found and no default driver specified?

You can check the drivers which are installed on your system by going to the ODBC Data Source Administrator. To open it, press ⊞ Win + R , and type in: odbcad32.exe . Then check the tab Drivers for installed drivers. The Name column indicates the exact name you should use in your connection string or DSN.

How do I know if ODBC driver is installed on SQL Server?

Open the Windows Control Panel. Open the Administrative Tools folder. Double-click Data Sources (ODBC) to open the ODBC Data Source Administrator window. Click the Drivers tab and locate the SQL Server entry in the list of ODBC drivers to confirm that the driver is installed on your system.


2 Answers

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

like image 94
vee Avatar answered Oct 02 '22 14:10

vee


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.

like image 41
o.v Avatar answered Oct 02 '22 12:10

o.v