Home » Php » db2 – php script for selecting/inserting, only succeeds for one row

db2 – php script for selecting/inserting, only succeeds for one row

Posted by: admin February 25, 2020 Leave a comment

Questions:

I made a fairly simple script to take records from a development database, and for each selected record insert it into production and get the newly created ID.

This “Works” but only for one record. WHen I run this script, it successfully connects, selects, inserts, and prints the newly inserted record’s ID but then the script just stops and when I check the database only one new record is there, even though there are over 500 records in the source table

Is it just becuase I’m using While instead of foreach? I think I’ve done something similar before with a while loop but this isjust dying on me after one successful attempt.

if($DB2connDEV && $DB2connPROD){

    $getDevelopment = "
        SELECT * FROM TEST_TABLE; // there are over 500 records in here
    ";

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

    while($gettingDevelopment = odbc_fetch_array($stmt)){

        $originalID = $gettingDevelopment['identity'];

        $insertTable = "INSERT INTO testing_insert_php (name) VALUES ($originalID)";
        $getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

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


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

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