sábado, 9 de febrero de 2013

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 Dependencies
$ 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...


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>");
          }
}
?>



As you can see...not too hard...even for a Windows boy -:D

Greetings,

Blag.