How To Connect PHP With SAP Hana

thumbnail

Hi Guys, welcome back to Codingofcents. Today we will learn how to connect PHP to SAP Hana database because it’s interesting if we can connect our web application with a database that developed by SAP.

SAP HANA is an in-memory, column-oriented, relational database management system developed and marketed by SAP SE. Its primary function as a database server is to store and retrieve data as requested by the applications

Before we continue, make sure you had install SAP Hana Client or you can download from SAP website. Make sure you have an account for login too ✌ ✌ ✌ . So bellow is the example connection and how we select data from database.

<?php
 
$driver = "HDBODBC";
$servername  = "SOMESERVER:30015";
$db_name = "DBNAME";
$username = "USERNAME";
$password = "PASSWORD";
 
try {
    $conn = new PDO("odbc:Driver=$driver;ServerNode=$servername; 
                  Uid=$username;Pwd=$password;CHAR_AS_UTF8=true;");
 
    $stm = $conn->query("SELECT * FROM \"DBNAME\".\"OITM\"  LIMIT 10 ");
    $arr = [];
    while ($row = $stm->fetch(PDO::FETCH_ASSOC)){
        $arr[] = [
            "ItemCode" => $row['ItemCode'],
            "ItemName" => $row['ItemName'],
            "FrgnName" => $row['FrgnName'],
        ];
    }
    echo json_encode($arr);
} catch (Exception $e) {
    echo json_encode($e->getMessage());
}

As you can see we add CHAR_AS_UTF8=true at the end of connection and it will solve issue that no data fetch when run query. And here the result

Connect PHP with SAP Hana
Connect PHP with SAP Hana

conclusion

Therefore, We can use any programming language to connect to SAP Hana Database that supports driver provided by Hana client installation process done.

Sponsored Links
Hi, my name is I Wayan Tisna Adi Muliarta. You can call me Tisna. I am the author of the Coding Of Cents

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × 1 =

Back To Top