Home » Php » php – loop is increasing each time through select/inserts

php – loop is increasing each time through select/inserts

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a script that is pretty straight forward in theory, where I”m trying to select relational data on a development DB2 database, and insert into a separate production db2 server (creating new IDs and relationships on the production side in the process).

So in this script, I:

  • Select from the dev ITEM table
  • Store the original primary key
  • insert the data into the dev ITEM table
  • Get the ID of the newly inserted one
  • select SUBITEM from dev with the original ID
  • for each subitem I insert the data into the prod SUBITEM table with the newly created ITEM ID as the relationship

The problem is the script actually runs successfully, but as it goes, it simply increases by one so even though it inserts each ITEM correctly, it ends up just getting EVERY subitem in the database and storing the original (1st row’s) ID. FOr instance:

My first ITEM select returns ID 1204. By the time I let it run for several items, my ITEM table shows records with IDs: 1204,1205,1206 and 1207

That’s correct, but when I look at the subITEMS table, I have 10 subITEM records all with itemID of 1204. So it got the right subitem for each iteration of ITEMS but only ever inserted the very first newID variable.

What did I do wrong here:

if($DB2connDEV && $DB2connPROD){

    $getDevItems = "
                SELECT 
                    itemt_ID,
                    DESCRIPTION
                FROM itemt 
            ";

    $stmt = odbc_exec($DB2connDEV, $getDevItems);

    while($gettingDevItems = odbc_fetch_array($stmt)){
        $rows[] = $gettingDevItems;
    }

    foreach($rows as $row){
        $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO itemt (itemt_id, description) VALUES(?,?)");

        $originalID = $row['itemt_ID'];
        $description = $row['DESCRIPTION'];

        $insertTable = odbc_execute($prepInsert, array($description));

        //Get newly created ID       
        $getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

        $stmt = odbc_exec($DB2connPROD, $getIdentity);
        $row = odbc_fetch_array($stmt);
        $newID = $row['LASTID'];

        if($newID) {
            echo "Last Insert ID is : " . $newID . "\n";
        } else {
            echo "No Last insert ID.\n";
        }

        //Get subItems and insert 
        $getSubItems = "SELECT NAME, DESCRIPTION
                FROM SUBITEMT
                WHERE itemt_ID = $originalID";

        $selectSubItems = odbc_exec($DB2connDEV, $getSubItems);

        while($gettingSubItems = odbc_fetch_array($selectSubItems)){
            $subItemRows[] = $gettingSubItems;
        }

        foreach($subItemRows as $subItemRow){
            $subItemPrepInsert = odbc_prepare($DB2connPROD, "INSERT INTO subitemt (itemt_id, NAME, DESCRIPTION) VALUES(?,?,?)");
            $subItemName = $subItemRow['NAME'];
            $subItemDescription = $subItemRow['DESCRIPTION'];

            $subtaskInsertExec = odbc_execute($subtaskPrepInsert, array($newID, $subItemName, $subItemDescription));

            //get newly created ID for subitem
            $getsubtaskID = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

            $subtaskIDSTMT = odbc_exec($DB2connPROD, $getsubtaskID);

            $newsubItemRow = odbc_fetch_array($subtaskIDSTMT);
            $newSubItemID = $newsubItemRow['LASTID'];

            if($newSubItemID) {
                echo "Last Insert subItem ID is : " . $newSubItemID . "\n";
            } else {
                echo "No Last insert subItem ID.\n";
            }
        }
    }

    odbc_close($DB2connPROD);
    odbc_close($DB2connDEV);
}
How to&Answers:

The problem is that you’re not clearing out $subItemRows between iterations of the main loop. So each time you’re adding the new subitems to the subitems from the previous iterations.

Put

$subItemRows = array();

before the loop:

while($gettingSubItems = odbc_fetch_array($selectSubItems)){
    $subItemRows[] = $gettingSubItems;
}