title: Connecting to MS SQL database with PHP: Data source name not found, and no default driver specified
body: <p>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.</p>
  
  <p>Everything is set up, as follows (the values between "" are correct in the file):</p>
  
  <p>/etc/odbc.ini:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>/etc/odbcinst.ini:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>/etc/freetds/freetds.conf:</p>
  
  <pre><code>[CRMSERVER]
  host = xxx.xxx.xxx.xxx 
  port = 1433
  tds version = 8.0
  </code></pre>
  
  <p>I doublechecked the host many times and it is correct. I also tried tds version 7.0, but no luck.</p>
  
  <p>I can succesfully connect to the server with isql:</p>
  
  <pre><code>root@crmart-web004:/# isql -v CRMCONNECT "user" "pass"
  +---------------------------------------+
  | Connected!                            |
  |                                       |
  | sql-statement                         |
  | help [tablename]                      |
  | quit                                  |
  |                                       |
  +---------------------------------------+
  SQL&gt;
  </code></pre>
  
  <p>But with php I just can't get it working, I get the following error:</p>
  
  <pre><code>[unixODBC][Driver Manager]Data source name not found, and no default driver specified 
  </code></pre>
  
  <p>My connectionstring:</p>
  
  <pre><code>$connection = odbc_connect("Driver={CRMCONNECT};Server=xxx.xxx.xxx.xxx;Database=dbname;","username","password");
  </code></pre>
  
  <p>All parameters are doublechecked and are correct.</p>
  
  <p>How come I can successfully connect with isql but it fails in php?</p>
  
  <p>My php version: </p>
  
  <pre><code>PHP Version 5.4.4-14+deb7u5
  </code></pre>
  
  <p>odbcinst configuration:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I hope someone has an idea what could be wrong.</p>
  
  <p>Thanks in advance</p>
  
  <p>Regards</p>
  
  <p>UPDATE:</p>
  
  <p>I changed my connectionstring to:</p>
  
  <pre><code>$connection = odbc_connect("CRMCONNECT;Database=dbname;","user","pass");
  </code></pre>
  
  <p>which is resulting in another error: </p>
  
  <pre><code>[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed
  </code></pre>
  
  <p>I'll be looking into that, thanks again vinodadhikary</p>
  
  <p>Regards.</p>
  
  <p>UPDATE 2:</p>
  
  <p>My connection string was wrong, it should have been:</p>
  
  <pre><code>$connection = odbc_connect("CRMCONNECT","user","pass");
  </code></pre>
  
  <p>Thanks vinodadhikary!</p>
  
  <p>Regards and happy holidays.</p>