Solved

Oracle Query into an MySQL DB Table using PHP

Posted on 2013-11-25
10
667 Views
Last Modified: 2013-12-19
I need to have a user click a button on a PHP page that then queries an Oracle database and instantly inserts the data into a table in a MySQL db table.  Trying to use this as a "snapshot in time" to compare to physical inventory.

I can connect and display data from both databases, no problem... but I am not sure how to go about inserting the query results in MySQL.

Any help is appreciated!
0
Comment
Question by:hydrazi
  • 5
  • 5
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Have you started writing the code yet?  If you can show us what you've written I'm sure we can help you complete it.  In particular if you're looking for some guidance on the MySQL side, this article may be useful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:hydrazi
Comment Utility
Thanks!  Yes, I am working on it now.  I have created a PDO object with the Oracle query data inside of it.... then I am going to create a PDO connection to the MySQL database...

And That's where I am stuck... do I do a WHILE statement to build an INSERT statement?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yes, I think you're on the right path.  You can use var_dump() to examine the data you get from Oracle.  That should help you see how to set up the query for MySQL.

Interesting app!
0
 

Author Comment

by:hydrazi
Comment Utility
Thanks!  We want to do an "offline" inventory from a snapshot.  Then 2 handheld Windows Mobile bar code scanners.  So, the pages have to be simple.  The scan the items into a table.  Separate tables then a report that compares them and downloads into an Excel spreadsheet.

I got about 30% of it working.  It will be 60% if I can get this part done.
0
 

Author Comment

by:hydrazi
Comment Utility
OK, here is my code for the query and insert into a table.  I am able to insert any of the fields that contain only NUMBERS (despite all fields in the MYSQL table being VARCHAR and the VAR_DUMP on the Oracle queried array showing as STRINGS.  If I add any of the fields that contain letters.... the insert doesn't work.  And I have no idea why.

<?php

//connect to Oracle
$conn = oci_connect('rrrr','xxxx','//99.99.99.99:1521/mvm.world'); 

//Test Oracle connection and verify
if (!$conn) { 
      $e = oci_error();   // For oci_connect errors pass no handle 
      echo '<b><font color="red">FAILED</font></b> : ' . htmlentities($e[
        'message']); 
} else { 
    // echo '<b><font color="green">Connection to Oracle: OK!</font></b>'; 
     
} 

//create the query to get inventory list from Oracle

$sql = "
SELECT T1.ObjectID,
T1.InvUnitLocation as Loction,
T1.InvUnitLot as Lot,
T1.InvUnitSubLot as Sublot,
T1.InvUnitResource as Resouce,
T1.Classif,
T2.Description,
T1.TotPrimQty as PrimQty,
T1.TotPrimQtyUM as PrimUM,
T2.CatCodesCode1 as Class,
T2.CatCodesCode2 as Subclass
FROM  inInventoryUnit T1, fdBasResc T2, inClassif T3
WHERE T1.InvUnitRescOID = T2.ObjectID
AND  T1.ClassifObjectID  =  T3.ObjectID
AND T2.CatCodesCode1 IN ('WIP','FINISHED GOODS')
AND T1.InvUnitInstType = 0
AND T1.InvUnitSite = 'MPM'
";

//Parse the connection and the query into $stid
$stid = oci_parse($conn, $sql);


//Connect to the MYSQL Database
$dbuser = "xxxx";
$dbpass = "xxxxxxx";
$conn2 = new PDO('mysql:host=localhost;dbname=shipinventory',$dbuser,$dbpass);   //PDO database connection

$check=(oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC));

//Loop through the Oracle results and insert each record.
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
    
  $sql2 = "INSERT INTO importlist (objectid,resource,sublot,primqty,description) VALUES ($row[OBJECTID],$row[RESOUCE],$row[SUBLOT],$row[PRIMQTY],$row[DESCRIPTION])";
  $resultx = $conn2->prepare($sql2);
  $resultx->execute();
    
}

oci_close($conn);

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
I think you might need to both prepare the query and bind the PHP variables into the query.  Please see the code examples in this article.  The important parts that you need will be found if you scan for these key phrases:
PDO - Error Visualization
PDO - Prepare a Query
Escape the Data and Load the Table

You also want to add error_reporting(E_ALL) to the top of the script and correct any things that generate notices.  I think you may find that you're missing some important quote marks.  Explanation here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
0
 

Author Comment

by:hydrazi
Comment Utility
Thanks, I got it to work but it worries me a bit.
The reason is that it refuses to insert unless I first assign some of the fields to variables and then insert the variables into MySQL.  I will try the error reporting to see if I can figure it out.

I think it has something to do with conversion.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
first assign some of the fields to variables and then insert the variables into MySQL
That is expected behavior if you're using bound variables in prepared queries.  The variable must exist before it can be bound to the query.
0
 

Author Comment

by:hydrazi
Comment Utility
Part of my issue seems to have been exactly what you said, Ray.  Thanks again!  Sorry for how long it took.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now