Geoff_S
Geoff_S

Reputation: 5107

Error doing db2 insert with PHP parameterized query

I'm trying to run a db2 parameterized query in PHP and upon the execution of the insert, I get the error: Invalid parameter number., SQL state S1002 in SQLDescribeParameter

This is my script:

        $getItems = "
        SELECT 
            ID,
            EXPIRATION_TIMESTAMP
        FROM table1
    ";

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

    $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");

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

        $rows[] = $gettingDevItems;
    }

    foreach($rows as $row){

        $originalID = $row['ID'];
        $expiration_timestamp = $row['EXPIRATION_TIMESTAMP'];


        $getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

        $insertTable = odbc_execute($prepInsert, array($originalID, $expiration_timestamp));//error at this line
        $insertTable = odbc_exec($DB2connPROD, $getIdentity);
        $row = odbc_fetch_array($stmt);
        $ret = $row['LASTID'];
    }

When I do a var_dump on the array of params, I get this:

array(2) {
  [0]=>string(1) "2"
  [1]=>string(26) "2019-10-03 00:00:00.000000"
}

What am I doing wrong here? Even if I take one value out to only insert one or the other I still get it, so It's not specific to one column.

Upvotes: 0

Views: 59

Answers (1)

Felippe Duarte
Felippe Duarte

Reputation: 15131

Maybe odbc can't support reuse of prepared statement, or your driver, or other part of your code, or another thing. Anyway, move the prepared statement inside your foreach loop to make sure you will rebuild it:

foreach($rows as $row){
   $prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");
...

Upvotes: 2

Related Questions