简体   繁体   中英

odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source

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.

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.

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://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM