Search code examples
phpodbchana

Trying to connect PHP to Hana DB through ODBC


I am trying to connect to my Hana DB using PHP 7.
Message returned is:
PHP Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect

This is what the ODBC Data Source Administrator looks like:
enter image description here

enter image description here

When I test the connection through the ODBC Data Source Admin it is successful.

PHP code:

$driver     = "HDBODBC";
$servername = "10.10.10.34";      //tried also hanabone here      
$db_name    = "NDB";                            
$username   = "ABC";                     
$password   = "123";

$connexion   = odbc_connect("Driver=$driver;ServerNode=$servername;Database=$db_name", $username, $password,SQL_CUR_USE_ODBC);

It is probably obvious, but what am I missing?


Solution

  • So ODBC clearly established (as you see the prompt successful when tested)

    For ODBC DSN (data source name) connection , try using PDO instead

    <?php
    
       $username = "ABC";
       $password = "123";
       $dsn ="odbc:hanabone";
    
    //For example to get all data from tablename1
     
      $queryString = "SELECT * from tablename1";
    
       $dbh = new PDO($dsn, $username, $password);
       $stmt = $dbh->prepare($queryString);
       $stmt -> execute();
       $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    ?>
    

    Alternatively, if you are using 32 bit driver and the server is accessed thru port 30015:

    <?php
    
    $driver     = "HDBODBC32"; 
    $servername  = "yourservername.com:30015"; 
    
    $db_name    = "HDB";        
    $username   = "ABC"; 
    $password   = "123"; 
    
    $conn       = odbc_connect("Driver=$driver;ServerNode=$servername;Database=$db_name;", $username, $password, SQL_CUR_USE_ODBC);