connect App Inventor with Oracle DB issue

150 views
Skip to first unread message

ibrahim bikdashi

unread,
Jan 5, 2019, 6:45:12 PM1/5/19
to MIT App Inventor Forum
hello i tried to change the code that connect appinventor with my sql to connect with oracle db

i tested on mysql it works  but i need to change the php code to with with oracle .
 i change the connection details and it connected perfect.but while i need to run any query i get an error

"Fatal error: Call to a member function query() on resource "
can some one help me ?
thank you in advance 

ABG

unread,
Jan 6, 2019, 2:05:31 PM1/6/19
to MIT App Inventor Forum
Do you have a PHP server connected to that Oracle database?

If so, search online for "Oracle PHP' to get the necessary 
drivers for that PHP server to make its connection.

To connect from AI2, you can send html containing php code, like
you might find in the above web search.

Note:  I have not done this myself,
this is a complex pain in the neck.

If you are just reading the data and not updating it,
and if it changes slowly, consider downloading
flat files from Oracle and posting them online
for Web download by your app or online spreadsheet access.

ABG


ibrahim bikdashi

unread,
Jan 8, 2019, 1:49:56 AM1/8/19
to mitappinv...@googlegroups.com
Hi ABG 
thank you for your reply yes i have a php server connected to that database .
i tested the php connection with oracle and it is working  but when i tried to send data to oracle i have this issue.
btw i am beginner programmer :) 

Note: my application goal is to send data to oracle .

the php code that i am using is the same code that Tiefun used to connect with mysql server. 


<?php


/************************************CONFIG****************************************/
//DATABSE DETAILS//
$DB_ADDRESS="";
$DB_USER="";
$DB_PASS="";
$DB_NAME="";

//SETTINGS//
//This code is something you set in the APP so random people cant use it.
$SQLKEY="secret";

/************************************CONFIG****************************************/

//these are just in case setting headers forcing it to always expire 
header('Cache-Control: no-cache, must-revalidate');

error_log(print_r($_POST,TRUE));

if( isset($_POST['query']) && isset($_POST['key']) ){                                   //checks if the tag post is there and if its been a proper form post
  //set content type to CSV (to be set here to be able to access this page also with a browser)
  header('Content-type: text/csv');

  if($_POST['key']==$SQLKEY){                                                           //validates the SQL key
    $query=urldecode($_POST['query']);
    if(get_magic_quotes_gpc()){     //check if the worthless pile of crap magic quotes is enabled and if it is, strip the slashes from the query
      $query=stripslashes($query);
    }
    $conn = new mysqli($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);    //connect

    if($conn->connect_error){                                                           //checks connection
      header("HTTP/1.0 400 Bad Request");
      echo "ERROR Database Connection Failed: " . $conn->connect_error, E_USER_ERROR;   //reports a DB connection failure
    } else {
      $result=$conn->query($query);                                                     //runs the posted query
      if($result === false){
        header("HTTP/1.0 400 Bad Request");                                             //sends back a bad request error
        echo "Wrong SQL: " . $query . " Error: " . $conn->error, E_USER_ERROR;          //errors if the query is bad and spits the error back to the client
      } else {
        if (strlen(stristr($query,"SELECT"))>0) {                                       //tests if it's a SELECT statement
          $csv = '';                                                                    // bug fix Undefined variable: csv
          while ($fieldinfo = $result->fetch_field()) {
            $csv .= $fieldinfo->name.",";
          }
          $csv = rtrim($csv, ",")."\n";
          echo $csv;                                                                    //prints header row
          $csv = '';

          $result->data_seek(0);
          while($row = $result->fetch_assoc()){
            foreach ($row as $key => $value) {
              $csv .= $value.",";
            }
            $csv = rtrim($csv, ",")."\n";
          }
          echo $csv;                                                                    //prints all data rows
        } else {
          header("HTTP/1.0 201 Rows");
          echo "AFFECTED ROWS: " . $conn->affected_rows;       //if the query is anything but a SELECT, it will return the number of affected rows
        }
      }
      $conn->close();                                          //closes the DB
    }
  } else {
     header("HTTP/1.0 400 Bad Request");
     echo "Bad Request";                                       //reports if the secret key was bad
  }
} else {
        header("HTTP/1.0 400 Bad Request");
        echo "Bad Request";
}
?>

ABG

unread,
Jan 8, 2019, 10:24:09 AM1/8/19
to MIT App Inventor Forum
To use MySQL connection code with an Oracle data base is an oversimplification,
like a cargo cult.

All the php Oracle connection samples I see use Oracle OCI
as the connection layer:
(See the Ajax parameter part, very important for passing data from AI2 to scripts)

There are simple connection test samples at
They are much easier to read than the MySQL code you used,
and specific to Oracle.

ABG





Reply all
Reply to author
Forward
0 new messages