使用 PHP 参数化查询执行 db2 插入时出错
Error doing db2 insert with PHP parameterized query
我正在尝试 运行 PHP 中的 db2 参数化查询,在执行插入时,出现错误:
Invalid parameter number., SQL state S1002 in SQLDescribeParameter
这是我的脚本:
$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'];
}
当我对参数数组执行 var_dump 时,我得到:
array(2) {
[0]=>string(1) "2"
[1]=>string(26) "2019-10-03 00:00:00.000000"
}
我在这里做错了什么?即使我取出一个值只插入一个或另一个我仍然得到它,所以它不特定于一列。
也许 odbc 不支持重用准备好的语句、您的驱动程序、您的代码的其他部分或其他东西。
无论如何,将准备好的语句移动到您的 foreach 循环中以确保您将重建它:
foreach($rows as $row){
$prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");
...
我正在尝试 运行 PHP 中的 db2 参数化查询,在执行插入时,出现错误:
Invalid parameter number., SQL state S1002 in SQLDescribeParameter
这是我的脚本:
$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'];
}
当我对参数数组执行 var_dump 时,我得到:
array(2) {
[0]=>string(1) "2"
[1]=>string(26) "2019-10-03 00:00:00.000000"
}
我在这里做错了什么?即使我取出一个值只插入一个或另一个我仍然得到它,所以它不特定于一列。
也许 odbc 不支持重用准备好的语句、您的驱动程序、您的代码的其他部分或其他东西。 无论如何,将准备好的语句移动到您的 foreach 循环中以确保您将重建它:
foreach($rows as $row){
$prepInsert = odbc_prepare($DB2connPROD, "INSERT INTO table2 (originalID, expiration_timestamp) VALUES(?,?)");
...