Search code examples
phpsqlodbc

PHP ODBC Connection to a SQL server from Ubuntu


My host is a ubuntu machine 20.04 I'm using PHP 7.4 with PDO_ODBC installed

The goal is to be able to access to a SQL database who is using Windows credential on a ubuntu machine.

This is my /etc/odbc.ini file :

[msodbcsql]
Description=SQL Server driver
Driver=/usr/lib/libmsodbcsql-17.so

[pds_database]
Driver=msodbcsql
Description=PDS database access
Server=<server ip>\PDS
Port=1433

When I run tsql -S pds_database -U <domain>\\<user> -P<passqord> I get this promt : 1>

I presume my pds_database connection is setup correctly ...

now, This is my PHP code :

$db = new PDO ("odbc:pds_database", '<domain>\<user>', '<password>');

I get the following error exception :

"SQLSTATE[IM002] SQLConnect: 0 [unixODBC][Driver Manager]Data source name not found and no default driver specified"

Do you have any idea what I missed during this connection ?

PS : /usr/lib/libmsodbcsql-17.so is a symbolic link to /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.7.so.2.1 What is mean I'm running a libmsodbcsql 64 bits like my PHP


Solution

  • I've the exactly same problem with you

    and I'm gonna share my full code about connection .php

    <?php
    
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    
    // define variables;
    /* $servername = "";
    $username = "";
    $password = "";
    $dbname = ""; */
    
    // Create connection
    try {
    
        // THIS IS OLD WRONG CODE, PLEASE CHECK BELOW
        $DSN = 'odbc:Driver={ODBC Driver 17 for SQL Server};
            Server=192.168.X.X,1234\\MSSQLSERVER;Database=db_um_1699;
            Trusted_Connection=yes';
    
        $dbconn = new PDO($DSN, 'username', 'userPassword');
        
        $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        "BRIDGE BUILD! \n <hr />";// Check connection
    
    }catch (PDOException $e) {
        echo "Broke: ". $e->getMessage();
    }
    

    And yet I'm still using old way to connecting which is a mess and so difficult to operated.


    12-16-2021 edit:

    I was changed odbc:driver to odbc:FreeTDS that worked!

    first assume we all had odbc drivers; we need to edit odbc.ini file in /etc/odbcinst.ini

    [FreeTDS]
    Description = ODBC for FreeTDS
    Driver64    = /usr/lib64/libtdsodbc.so
    Setup64     = /usr/lib64/libtdsodbc.so
    FileUsage   = 1
    

    next try to build the connection:

    // Create connection
    try {
    
        // CHANGE ODBC to FREETDS
        $DSN = 'odbc:Driver={FreeTDS};
            Server=192.168.X.X,1234\\MSSQLSERVER;Database=db_um_1699;
            Trusted_Connection=yes';
    
        $dbconn = new PDO($DSN, 'username', 'userPassword');
        
        $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        $dbconn -> setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        "BRIDGE BUILD! \n <hr />";// Check connection
    
    }catch (PDOException $e) {
        echo "Broke: ". $e->getMessage();
    }
    

    hope this information would be anyone who was same problem I had

    but this still maybe not the good enough for all kind similar problem since our database really aged which was MS-sql-2008