I've spent a week getting a very old script that runs between DB2 and MySQL condensed and functioning but I've been told the last few days on SO that my code is horrible and confusing and shouldn't work (all of which may be true).
The script below runs, it prints all of my arrays accordingly and it preforms the insert I need it to.
Anyway, I'm not asking for someone to solve a specific problem, but please, if you can, just gracefully and guidingly point out the issues in my code so that I can have a quantifiable comparison to make and take the feedback to make this code better and possibly better solve any future problems I have with it.
I'm brand new to working with DB2 and still trying to come into my own in PHP, so I'm just looking for some helpful guidance here.
<?php
//SELECT Order Ids from mysql table to see if status is 'S'
$orderShippedCheck = "
SELECT
order_id,
order_status
FROM order_status
WHERE order_status = 'S'
";
//get result and store in array
$result = mysqli_query($mysqlConn, $orderShippedCheck);
$order_ids = array();
//loop results to gather order IDs and store them
while ($row = mysqli_fetch_array($result)){
$order_ids[] = $row['order_id'];
}
print_r($order_ids); /*This prints the correct order IDs*/
$orderIdsStr = "'" . implode("', '", $order_ids) . "'";
//SELECT FROM DB2 WITH THE ORDER NUMBERS FIRST
$query = "SELECT invnoc as INVOICE,
cstnoc AS DEALER,
framec AS FRAME,
covr1c AS COVER,
colr1c AS COLOR ,
extd2d AS SHIPDATE,
orqtyc AS QUANTITY
FROM GPORPCFL
WHERE invnoc IN ({$orderIdsStr})
GROUP BY invnoc,cstnoc, slsnoc, orqtyc, framec, covr1c,colr1c, extd2d
ORDER BY invnoc asc";
$Db2ShipRslt = odbc_exec($DB2Conn, $query);
if ( $Db2ShipRslt === false ) {
exit (odbc_errormsg($DB2Conn));
}
//Process the results
$Db2ShipArr = array();
while($db2ShipRow = odbc_fetch_array($Db2ShipRslt))
{
//Output the record
print_r($db2ShipRow); /*This prints the correct Rows*/
//Append record to results array
$Db2ShipArr[] = $db2ShipRow;
}
foreach($Db2ShipArr as $Db2Ship){
//Check to see if there are any records in jfi_sales.placements for the dealer/sku combo just pulled from DB2
$dealerSkuCheck = "
SELECT
sku_id,
dealer_id
FROM placements_new p
INNER JOIN skus s
ON p.sku_id = s.id
WHERE p.dealer_id = '{$Db2Ship['DEALER']}'
AND s.frame = '{$Db2Ship['FRAME']}'
AND s.cover1 = '{$Db2Ship['COVER']}'
AND s.color1 = '{$Db2Ship['COLOR']}'
AND p.order_num = '{$Db2Ship['INVOICE']}'
";
//IF those records exist for the dealer/sku, store them in array
$existingCheckRslt = mysqli_query($mysqlConn, $dealerSkuCheck);
$existingRecords = array();
while ($existingRow = mysqli_fetch_array($existingCheckRslt, MYSQLI_ASSOC)){
$existingRecords[] = $existingRow;
}
$row_cnt = mysqli_num_rows($existingCheckRslt);
print_r($existingRecords); /*This prints the right records showing that there exists record(s) in placements that have that dealer_id and sku_id*/
//If the existingRecords array is empty, meaning no records exist for that dealer/sku, then preform the insert
if($row_cnt == 0){
//INSERT # of records equal to QUANTITY
$stmt = $PDO->prepare("
INSERT IGNORE INTO placements_new (sku_id, group_id, dealer_id, start_date, expire_date, locations, order_num)
SELECT
id,
sku_group_id,
:DEALER,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 7 DAY) as start_date,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPDATE, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date,
:QUANTITY,
:INVOICE
FROM skus s
WHERE s.frame=:FRAME AND s.cover1=:COVER AND s.color1=:COLOR
");
$PDO->beginTransaction();
$i = 0;
while($db2row = odbc_fetch_array($Db2ShipRslt)) {
if(++$i % 1000 == 0) {
$PDO->commit();
$PDO->beginTransaction();
}
$stmt->execute($db2row);
}
$PDO->commit();
}
}
//Close Connections
if (mysqli_close($mysqlConn)){
echo "MySQL Closed";
}
$PDO = null;
$stmt = null;
?>