0

I want to create connection with teradata DB form php. My code is hosted on Ubuntu server but the below error occurred.

"odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /path".

while it is working properly from local host.

php code is:

$sDriver = 'Teradata';
$sDBCName = 'ip';
$sDatabase = 'dbname';
$sDSN = "Driver={$sDriver};DBCName={$sDBCName};Database={$sDatabase};";
$connection = odbc_connect($sDSN, "username", "password");

but there is nothing wrong in the code bcoz it is working on local host.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Naveed Ul Islam
  • 55
  • 1
  • 12

2 Answers2

0

I also suffered from the same problem before. I somehow manage to fix it using this code:

$database = 'db';
$user = 'user';
$password = 'pass';
$hostname = 'ip';
$port = portNo;
$db = odbc_connect("Driver={Your-Driver};HOSTNAME=$hostname;
                    Database=$database;PORT=$port;PROTOCOL=TCPIP;", $user, $password);

Hope it helps.

Miggy
  • 816
  • 7
  • 16
  • This code is working fine on local host but when i try to run it from server it's create problem, can you plz confirm what configuration needed on server(ubuntu seerver)? – Naveed Ul Islam Nov 08 '17 at 09:49
  • @NaveedUlIslam what was the error that you are currently expriencing? – Miggy Nov 08 '17 at 10:20
  • this error: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /path – Naveed Ul Islam Nov 08 '17 at 10:34
  • Maybe its in your server configuration. It says in your error that there is not default driver specified. – Miggy Nov 08 '17 at 10:37
  • Yes @miggy now what should be need on server to resolve this issue any idea? – Naveed Ul Islam Nov 08 '17 at 10:40
  • @NaveedUlIslam maybe this could help https://stackoverflow.com/questions/21237678/how-to-fix-the-unixodbcdriver-managerdata-source-name-not-found-and-no-defa – Miggy Nov 08 '17 at 10:43
  • Your server needs to have the same ODBC source specified as does your local machine. – access_granted Nov 08 '17 at 22:18
  • @access_granted, what configuration i need on server for resolving this issue? – Naveed Ul Islam Nov 10 '17 at 06:56
  • You need an ODBC driver, and the same DNS name as the one you have on the client. Then the same code should work on both nodes. – access_granted Nov 13 '17 at 22:12
0

You would need to install both PHP ODBC extension and Microsoft's ODBC drivers for SQL server.

Install PHP ODBC Extension

sudo apt-get install php7.2-odbc

# Note that on Ubuntu 16.04 and earlier you'll need to register this PPA first.

sudo add-apt-repository ppa:ondrej/php

Install the Microsoft ODBC Driver 17 for SQL Server

Import GPG keys:

bash curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

Register the Microsoft Ubuntu repository:

curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

# Note replace 16.04 in package URL aboive with your Ubuntu version. i.e. 14.04, 18.04, etc.

Download the drivers for your version of Ubuntu:

bash sudo ACCEPT_EULA=Y apt-get install msodbcsql17

Install optional bcp and sqlcmd tools:

bash sudo ACCEPT_EULA=Y apt-get install mssql-tool

Connect from your PHP application

<?php
$dsn = 'Driver={ODBC Driver 17 for SQL Server};Server=server.domain;Database=database_name';
$username = 'your_username';
$password = 'your_password';
if($connection = odbc_connect($dsn, $username, $password, SQL_CUR_USE_ODBC)) {
    echo "Connected to the datasource.";
} else {
    echo "Could not connect to the datasource.";
}

Reference: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

user4603841
  • 1,246
  • 9
  • 8