我正在尝试使用PDO在php脚本中准备DB2插入,这给了我错误“参数标记或NULL无效”
我正在运行一个选择查询以获取值并将其设置为参数,然后设置了我的插入语句。我准备了它,并在基于第一个查询的访存的while循环中,设置了参数,以便能够使用所需的值运行插入。问题是它在我准备插入的行上失败了。
第一个查询运行并打印
[ORDERNUMBER] 123456
[CUSTOMER] 1
[PRODUCT] 123
[FABR] A
[COLOR] 1
[SHIPDATE] 20181119
所以数据又回来了,我什至开始转换数据类型,但仍然出现错误。
这是打印以上内容的初始选择:
//SELECT query to get source data
$getOrderDetails = "
SELECT
cast(invnoc as int) as ORDERNUMBER,
cast(cstnoc as int) AS CUSTOMER,
cast(PRODUCTc as int) AS PRODUCT,
cast(covr1c as int) AS FABR,
cast(colr1c as int) AS COLOR ,
cast(extd2d as varchar(45)) AS SHIPDATE,
FROM testTable g
";
try {
$orderDetailCheck = $DB2conn->prepare($getOrderDetails);
$detailRslt = $orderDetailCheck->execute();
$orderDetailCount = $orderDetailCheck->fetch();
print_r($orderDetailCount);
}catch(PDOException $ex) {
echo "QUERY ONE FAILED!: " .$ex->getMessage();
}
插入声明:
$insertPlacement = "
INSERT INTO table (CUSTOMER_id, start_date, expire_date)
SELECT DISTINCT
:CUSTOMER,
(to_date(:SHIPDATE, 'YYYYMMDD') + 7 DAYS) as start_date,
(to_date(:SHIPDATE, 'YYYYMMDD') + 127 DAYS) as expire_date
from OFSTable ofs
WHERE ofs.PRODUCT=:PRODUCT AND ofs.FABR1=:FABR AND ofs.color1=:COLOR;
";
设置参数:
//this line is failing
$insert = $DB2conn->prepare($insertPlacement);
while ($row2 = $orderDetailCheck->fetch(PDO::FETCH_ASSOC)) {
$insertParams = [
":CUSTOMER" => $row2["CUSTOMER"],
":ORDERNUMBER" => $row2["ORDERNUMBER"],
":PRODUCT" => $row2["PRODUCT"],
":FABR" => $row2["FABR"],
":COLOR" => $row2["COLOR"],
];
我意识到问题是我不能将参数称为列名或表名,但这在mysql中有效,不适用于db2。我需要为:CUSTOMER参数插入值,但它的行为就像我不能那样做。我应该如何更改它以使其像在Mysql上一样在db2上工作?
答案 0 :(得分:1)
您应该强制转换所有参数。像这样:
storageBucket
来源: