Skip to Content

PHP rocks on SAP HANA too! (Linux version)

If you know me…you know I’m not a Linux boy…however…I still have my laptop LG T1 Express Dual that I bought on 2007…for my first SAP TechEd ever…as you may assume…running Windows and installing more software on this laptop was a nice way to slowly kill it…so the laptop stayed on my closed for a long time…until I decided to install Ubuntu on it and bring to live once again 🙂

What they say it’s true…even the crappiest and oldest laptop will behave great on Linux…and BTW, I’m writing this blog on my Linux box 😉

A couple of weeks ago I wrote a blog called PHP rocks on SAP HANA too! as I realized that I haven’t blog about PHP and SAP HANA and also because I knew that a lot of people were facing issues trying to make it work together…but…I wrote for Windows…and Linux users are still struggling to get this working…so…time for a new blog…

I gotta say…without this awesome blog HANA with odbc on Ubuntu 12.04 written by Ethan Zhang I will be probably still struggling with the connection details…but anyway…not being a Linux boy…I thought it would be a good idea to detail all the steps and problems that I overcome to finally make it work…

Of course…I didn’t have PHP installed on my Linux box…so the first step was install it…however, I didn’t want to spend so much time installing everything separately, so I decided to use LAMP.

LAMP Installation

$ sudo apt-get install tasksel

$ sudo tasksel install lamp-server

After this two simple lines…PHP was up and running…so next step was install the SAP HANA Client. 32bits Linux Version.

Now, I needed something to connect to my SAP HANA Server…so unixODBC was the best choice.

unixODBC Installation

$ sudo apt-get install unixODBC unixODBC-dev

With this, it was time to configure the ODBC connection…

odbc.ini configuration

$ sudo vi odbc.ini

[HDB]

driver = /usr/sap/hdbclient32/libodbcHDB32.so

ServerNode = hana_server:30115

It was time to the first test…so I did the following…

ODBC Testing
$ isql -v HDB SYSTEM manager

After this…I got an error saying that libodbcHDB32.so couldn’t be found because the file or directory didn’t exist…it was weird…I decided to take a look at the dependencies…

Checking dependecies
$ ldd /usr/sap/hdbclient32/libodbcHDB32.so

This point me out to the fact that libaio.so wasn’t found on my system…so using the Ubuntu Software System…I just installed it…

/wp-content/uploads/2013/02/libaio_182845.png

My next test of isql was successful, so…being kind of lazy…I just copied the code from my other blog and run it 😉

PHP_HANA.php

<?php

$conn = odbc_connect(“HDB”,”SYSTEM”,”manager”,SQL_CUR_USE_ODBC);

if (!($conn))

{

          echo “<p>Connection to DB via ODBC failed: “;

        echo odbc_errormsg ($conn);

        echo “</p>\n”;

}

else{

          if(isset($_POST[“CARRID”]) == false){

                  $sql = “SELECT CARRID, CARRNAME FROM SFLIGHT.SCARR WHERE MANDT = 300”;

               $rs = odbc_exec($conn,$sql);

               print(“<DIV ALIGN=’CENTER’>”);

               print(“<H1>SAP HANA from PHP</H1>”);

               print(“<FORM NAME=’Get_Data’ ACTION=’$_SERVER[PHP_SELF]’ METHOD=’POST’>”);

               print(“<SELECT NAME=’CARRID’>”);

               while($row = odbc_fetch_array($rs)){

                     $carrid = $row[“CARRID”];

                     $carrname = $row[“CARRNAME”];

                     print(“<OPTION VALUE=’$carrid’>$carrname”);

               }

        print(“</SELECT>”);

        print(“<INPUT TYPE=’SUBMIT’ VALUE=’Get Data’>”);

        print(“</FORM>”);

        print(“</DIV>”);

        }

        else{

                  $carrid_param = $_POST[“CARRID”];

                $sql = “SELECT * FROM \”_SYS_BIC\”.\”blag/AV_FLIGHTS\”

                        WHERE CARRID = ‘$carrid_param'”;

                $rs = odbc_exec($conn,$sql);

                print(“<DIV ALIGN=’CENTER’><TABLE BORDER=1>”);

                print(“<TR><TH>MANDT</TH><TH>CARRID</TH><TH>CONNID</TH>

                       <TH>COUNTRYFR</TH><TH>CITYFROM</TH>

                       <TH>AIRPFROM</TH><TH>COUNTRYTO</TH>

                       <TH>CARRNAME</TH><TH>DISTANCE</TH></TR>”);

                while($row = odbc_fetch_array($rs)){

                          $mandt = $row[“MANDT”];

                $carrid = $row[“CARRID”];

                $connid = $row[“CONNID”];

                $countryfr = $row[“COUNTRYFR”];

                $cityfrom = $row[“CITYFROM”];

                $airpfrom = $row[“AIRPFROM”];

                $countryto = $row[“COUNTRYTO”];

                $carrname = $row[“CARRNAME”];

                $distance = $row[“DISTANCE”];

                print(“<TR><TD>$mandt</TD><TD>$carrid</TD>

                         <TD>$connid</TD><TD>$countryfr</TD>

                         <TD>$cityfrom</TD><TD>$airpfrom</TD>

                         <TD>$countryto</TD><TD>$carrname</TD>

                         <TD>$distance</TD></TR>”);

               }

               print(“</TABLE>”);

               print(“<A HREF=’PHP_HANA.php’>Go Back</A></DIV>”);

          }

}

?>

PHP_HANA_Linux_001.png

PHP_HANA_Linux_002.png

As you can see…not too hard…even for a Windows boy 😛

12 Comments
You must be Logged on to comment or reply to a post.
  • Hi Alvaro,

    Linux – Experts out there may get jealous now 😉 . I think you should leave something for them also 😆

    Gr8 blog as usual, but this time it came after long gap.  🙂

    Regards

    Kumar.

    • Thanks Kumar 🙂 Just trying to do my job as always 😉

      Linux is really nice…I just wish I have more knowledge about it…most of my time is spend on Google than on the actual “blog” job 😛

      Greetings,

      Blag.

  • Hi,

    Actually I can connect to the HANA database, but when I try to use odbc_result or odbc_fetch_array, the page crash like the connection has lost.

    Could you help me?

    • Alejandro:

      Then I assume that you can connect via ISQL right? Please make sure you’re using SQL_CUR_USE_ODBC for the connection statement and that your query is syntactically correct…check the logs as well…can you post the text of the error?

      Greetings,

      Blag.

      Development Culture.

      • [SOLVED]

        I am using UBUNTU 10.04, upgrade to 12 with apache 2.2 and works like a charm

        Thanks for your help

        ————————-

        Hi Thanks for the answer

        I am using UBUNTU and try to connect to my HANA server in SUSE

        Error log:

        [Tue Sep 02 13:10:08 2014] [notice] child pid 1232 exit signal Segmentation fault (11)

        [Tue Sep 02 13:10:08 2014] [notice] child pid 1233 exit signal Segmentation fault (11)

        [Tue Sep 02 13:10:09 2014] [notice] child pid 1234 exit signal Segmentation fault (11)

        [Tue Sep 02 13:10:58 2014] [notice] child pid 1230 exit signal Segmentation fault (11)

        [Tue Sep 02 13:10:58 2014] [notice] child pid 1236 exit signal Segmentation fault (11)

        [Tue Sep 02 13:10:58 2014] [notice] child pid 1784 exit signal Segmentation fault (11)

        [Tue Sep 02 13:11:26 2014] [notice] child pid 2141 exit signal Segmentation fault (11)

        [Tue Sep 02 13:11:26 2014] [notice] child pid 2142 exit signal Segmentation fault (11)

        [Tue Sep 02 13:11:27 2014] [notice] child pid 2143 exit signal Segmentation fault (11)

        My code is

        $conn = odbc_connect(“HDB”,”SYSTEM”,”SYSTEM”, SQL_CUR_USE_ODBC);

        if (!($conn))

        {

            echo “<p>Error de conexion: “;

            echo odbc_errormsg ($conn);

            echo “</p>\n”;

        }

        else

        {

            print(“<H1>SAP HANA en PHP</H1>”);

            $tablelist = odbc_tables($conn);

            while (odbc_fetch_row($tablelist))

            {

                echo odbc_result_all($tablelist);

            }

            odbc_free_result($tablelist);

            odbc_close($conn);

             

            $sql = “SELECT * FROM TEST_TST.OCRD”;

            $rs = odbc_exec($conn, $sql);

            if (!$rs)

            {

                exit(“Error en SQL”);

            }

            //$compname=odbc_result($rs,1);

            print(“<DIV ALIGN=’CENTER’>”);

            print(“<SELECT NAME=’CardName’>”);

            while($row = odbc_fetch_array($rs))

            {

                  print_r($row);

               //   echo $row[0];

               //$cardname = odbc_result($rs, 1);

               $cardname = $row[“CardName”];

               print(“<OPTION VALUE=’$cardname’>$cardname”);

            }

            print(“</SELECT>”);

            print(“<INPUT TYPE=’SUBMIT’ VALUE=’Get Data’>”);

            print(“</FORM>”);

            print(“</DIV>”);

             

        odbc_close($conn);

        }

        When I try to use odbc_fetch_row, odbc_result or odbc_fetch
        _array the page crashes.

        • So in the end it was just an “update” issue? Awesome that you fix it…but those kind of errors are the worse 🙁

          Anyway…thanks to you people will check their current versions and update if needed 🙂

          Thanks a lot for that! 😀

          Greetings,

          Blag.

          Development Culture.

  • Is there anyway to make this work with a local odbc.ini file? I am trying to follow your steps on an OpenShift account and “sudo vi odbc.ini” is not permitted.

  • Hello,

    Thank you for this post.

    For SAP Hana I want to precise the code syntaxe because the exemple you give are not working.

    Is working :
    $sql = SELECT CardCode FROM YOUR_INSTANCE.OCRD;

    You must notice the ‘ that suround the code and use ” to suround fields and instance name.

    Here is an exemple of code

    <?

    //DNS delared in the /etc/odbc.ini of Debian or Umbutu
    $db_DNS = “HDB”;
    // Username (SYSTEM is the default user name)
    $username = “SYSTEM”;
    // Password
    $password = “YourPassWord”;

    $conn = odbc_connect($db_DNS, $username, $password, SQL_CUR_USE_ODBC);

    if (!$conn) {
    // Try to get a meaningful error if the connection fails
    echo “Connection failed.\n”;
    echo “ODBC error code: ” . odbc_error() . “. Message: ” . odbc_errormsg();
    } else {
    $sql = ‘SELECT “CardCode” FROM “SBO_YOURDATABASE”.”OCRD”‘;
    $result = odbc_exec($conn, $sql);
    if (!$result) {
    echo “Error while sending SQL statement to the database server.\n”;
    echo “ODBC error code: ” . odbc_error() . “. Message: ” . odbc_errormsg();
    } else {
    while ($row = odbc_fetch_object($result)) {
    $Fetch_Array = odbc_fetch_array($result);
    $Test = $Fetch_Array[“CardCode”] . “<br>”;
    echo “CardCodeSAP: “.$Test . “<BR>”;
    }
    }
    odbc_close($conn);
    }
    ?>

    Patrice

    • Dear Patrice:

      Thanks for you comment and your fix 🙂 This is already an old post…so I haven’t check it in a long time…when I made it…everything was working fine…although some things might have changed or maybe I had a typo…anyway…thanks for your collaboration 🙂

      Greetings,

      Blag.

      Development Culture.