DB2,在odbc和mysql之间传递
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DB2,在odbc和mysql之间传递相关的知识,希望对你有一定的参考价值。
我正在尝试执行以下程序,但是我遇到了一些错误(我用/ ** /注释了有错误的行,而正常的注释有//)
基本上我是:
- 从状态不是'S'的mysql表中选择记录
- 保存数组中这些记录的ID和状态代码
- 从具有相同订单号的odbc连接上的表中提取记录(Mysql是order_id,odbc / db2是INVNOZ作为ORDERNUM)
- 比较每个记录的两个表之间的状态
- 循环查看db2连接上的状态是否仍然相同,或者是否已更改为“S”
- 对于已更改为“S”的订单,请将这些订单号存储在另一个数组中
所以它正在努力开始处理DB2,我认为它很多只与我如何在数据库之间使用变量有关。
也许这是语法和不正确的变量使用之间的混合,但我不确定从这里开始的确切位置。我已经清除了一些其他错误,但我仍然坚持这些。我可以回答任何问题来澄清
//Selecting Records with status other than 'S'
$preShipmentOrders = "
SELECT
order_id,
order_status
FROM order_status
WHERE order_status <> 'S'
";
$preShipResult = mysqli_query($mysqlConn, $preShipmentOrders);
//Store all Order_id and order_status for records with status other than 'S'
$PSOrderIds = array();
while ($row = mysqli_fetch_array($preShipResult))
{
$PSOrderIds[] = $row['order_id'];
$PSOrderIds[] = $row['order_status'];
}
print_r($PSOrderIds); //Up to this line works fine, prints correct Ids/Statuses
foreach($PSOrderIds as $PSOrderId){
//Pull records from DB2 where INVNOZ = previous order_id and check to see if FSTATZ in DB2 is same status as previous query, or if it's now 'S' in DB2
$orderCompareDB2 = "
SELECT
INVNOZ AS ORDERNUM,
FSTATZ AS STATUS
FROM GPORTAFL
WHERE ORDERNUM = $PSOrderIds[order_id] /*This gives - Notice: Undefined index: order_id in C:check.php on line*/
";
$compareResult = odbc_exec($DB2Conn,$orderCompareDB2); /*Error - odbc_exec(): SQL error: [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0104 - Token <END-OF-STATEMENT> was not valid. Valid tokens: ( + - ? : DAY INF NAN NOT RID ROW., SQL state 37000 in SQLExecDirect in C:check.php on line*/
//If any of those db2 records now have 'S' in fstatz, store those order IDs/INVNOZ
$matchedOrderIds = array();
while($compareRow = odbc_fetch_array($compareResult)){ /*Error - odbc_fetch_array() expects parameter 1 to be resource, boolean given in C:check.php on line*/
if($compareRow[STATUS] == 'S'){
$updatePlacement = "UPDATE jfi_sales.order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id IN (" . implode(',',$matchedOrderIds) .") ";
if(mysqli_query($mysqlConn, $updatePlacement)){
echo "Update Successful";
}else{
echo "ERROR: Couldn't update. " . mysqli_error($mysqlConn);
}
}else{
echo "FAILED";
}
}
}
你的循环需要存储当前数据,然后foreach循环的每次迭代都将指向该元素,然后你可以从那里的数据中挑选出orderID ...
while ($row = mysqli_fetch_array($preShipResult))
{
$PSOrderIds[] = $row;
}
foreach($PSOrderIds as $PSOrderId){
$orderCompareDB2 = " SELECT
INVNOZ AS ORDERNUM,
FSTATZ AS STATUS
FROM GPORTAFL
WHERE INVNOZ = '{$PSOrderId['order_id']}'
";
$compareResult = odbc_exec($DB2Conn,$orderCompareDB2);
if ( $compareResult === false ) {
exit (odbc_errormsg($DB2Conn));
}
...
第二个错误是由于语句以ORDERNUM =
结尾,因为替换失败,第三个错误是由于语句没有正确编译。
如果SQL无法编译,还会添加一些错误报告(尽管我讨厌使用exit()
,但通常不应该触发它。
或者加载的快捷方式是
$PSOrderIds= mysqli_fetch_all($preShipResult,MYSQLI_ASSOC);
那是因为你从来没有真正在$PSOrderIds
中构建一个名为order_id
的密钥。你可以通过运行var_dump($PSOrderIds)
来确认这个解决方案是使你的while()
循环与$PSOrderIds[] = array('order_id'=>$row['order_id'], 'order_status'=>$row['order_status']);
适应
你的第二个错误; /*Error - odbc_fetch_array() expects parameter 1 to be resource, boolean given in C:check.php on line*/
是因为你的第一个查询失败了。并且第一个查询失败,因为它从来没有为ORDERNUM
获得正确的值
以上是关于DB2,在odbc和mysql之间传递的主要内容,如果未能解决你的问题,请参考以下文章