我正在尝试从数据库运行两个查询。我需要两个查询来创建一个具有相同列名的数组,然后将两个数组合并在一起。
以下是我的两个问题:
## Order History
$q47 = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY,
QTYRC AS RQTY FROM AMFLIBP.MOHMST WHERE FITEM='$id' AND FITWH='$wh'";
$stmt47 = db2_prepare($con, $q47);
$result47 = db2_execute($stmt47);
$q48 = "SELECT DOKDT AS COMPDT, ORDNO AS ORDER, QTYOR AS OQTY,
DKQTY AS RQTY, VNDNR AS VNDR FROM AMFLIBP.POHISTI
WHERE ITNBR='$id' AND HOUSE='$wh'";
$stmt48 = db2_prepare($con, $q48);
$result48 = db2_execute($stmt48)
我想对两个查询做一个while语句。
所以,看起来像这样:
while($fin47 = db2_fetch_assoc($stmt47));
and
while($fin48 = db2_fetch_assoc($stmt48));
在while语句完成后,
我想从两个while语句中获取结果
将它们放入按COMPDT
排序的数组中。
我试过这样做:
## Order History
$q47 = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY, QTYRC AS RQTY
FROM AMFLIBP.MOHMST
WHERE FITEM='$id' AND FITWH='$wh'";
$stmt47 = db2_prepare($con, $q47);
$result47 = db2_execute($stmt47);
while($fin47 = db2_fetch_array($stmt47));
$q48 = "SELECT DOKDT AS COMPDT, ORDNO AS ORDER, QTYOR AS OQTY, DKQTY AS RQTY,
VNDNR AS VNDR FROM AMFLIBP.POHISTI
WHERE ITNBR='$id' AND HOUSE='$wh'";
$stmt48 = db2_prepare($con, $q48);
$result48 = db2_execute($stmt48);
while($fin48 = db2_fetch_array($stmt48));
$array = array_merge($fin47, $fin48);
print_r($array);
来自print_r($array);
我的php.log
中只有以下错误:
[29-Dec-2013 09:17:03 Antarctica/Mawson] PHP Warning:
array_merge(): Argument #1 is not an array in
/www/zendsvr6/htdocs/views/test.php on line 42
我知道while语句导致问题,因为我之前已经完成了array_merge。 这与我的语法有关。有任何想法吗?提前谢谢!
增加信息:
通过这样做:
## Order History
$q47 = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY, QTYRC AS RQTY
FROM AMFLIBP.MOHMST WHERE FITEM='$id' AND FITWH='$wh'";
$stmt47 = db2_prepare($con, $q47);
$result47 = db2_execute($stmt47);
while($fin47 = db2_fetch_array($stmt47))
{var_dump($fin47);}
我明白了:
array(4) { [0]=> string(7) "1090225" [1]=> string(7) "M703760"
[2]=> string(6) "20.000"
[3]=> string(6) "20.000" } array(4) { [0]=> string(6) "960614"
[1]=> string(7) "M802110" [2]=> string(7) "340.000"
[3]=> string(4) ".000" }
另外,当我尝试下面的建议:
$q47 = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY, QTYRC AS RQTY
FROM AMFLIBP.MOHMST WHERE FITEM='$id' AND FITWH='$wh'";
$stmt47 = db2_prepare($con, $q47);
$result47 = db2_execute($stmt47);
while($fin47 = db2_fetch_array($stmt47))
{$array[] = $fin47;}
print_r($array);
我明白了:
Array ( [0] => Array ( [0] => 1090225 [1] => M703760 [2] => 20.000 [3] => 20.000 )
[1] => Array ( [0] => 960614 [1] => M802110 [2] => 340.000 [3] => .000 ) )
哪个有效但丢失了我的专栏名称?
答案 0 :(得分:2)
这一行:
while($fin47 = db2_fetch_array($stmt47));
会使用$fin47
中的下一行覆盖SELECT
的每个值。你应该做的是:
$fin47 = array();
while($fin47[] = db2_fetch_array($stmt47));
然而,这似乎是一种不必要的复杂方式。您可以使用一个查询以所需的形式提取数据,这意味着不需要所有PHP代码。
$fin47 = array();
$query = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY, QTYRC AS RQTY, null as VNDR FROM AMFLIBP.MOHMST WHERE FITEM='$id' AND FITWH='$wh'".
" UNION ALL ".
"SELECT DOKDT AS COMPDT, ORDNO AS ORDER, QTYOR AS OQTY, DKQTY AS RQTY, VNDNR AS VNDR FROM AMFLIBP.POHISTI WHERE ITNBR='$id' AND HOUSE='$wh'".
" ORDER BY COMPDT";
$stmt = db2_prepare($con, $query);
$result = db2_execute($stmt);
while($fin47[] = db2_fetch_array($stmt));
注意:我在第一个VNDR
中添加了SELECT
的虚拟值,以匹配UNION
的列数。在结果数组中,这些将显示为空值。
答案 1 :(得分:1)
此循环将在每次运行时覆盖您的变量。它不会收集行。在最后一次运行中,它会在false
分配后离开。
while($fin47 = db2_fetch_array($stmt47));
你应该做的是:
while($fin47 = db2_fetch_array($stmt47)) {
$array[] = $fin47;
}
在你的第二个while
中你可以附加到同一个数组。因此,您的合并变得多余。
答案 2 :(得分:-1)
$fin47
和$fin48
的原始输出是什么?
您是否尝试对值进行简单的+
:
$array = $fin47 + $fin48;
编辑然后为什么不这样做。我添加了名为$fin47_results
和$fin47_results
的数组,并在每个查询中设置while
循环,以将$fin47
和$fin48
的值添加到它们中:
## Order History
$q47 = "SELECT OCODT AS COMPDT, ORDNO AS ORDER, ORQTY AS OQTY, QTYRC AS RQTY FROM AMFLIBP.MOHMST WHERE FITEM='$id' AND FITWH='$wh'";
$stmt47 = db2_prepare($con, $q47);
$result47 = db2_execute($stmt47);
$fin47_results = array();
// while($fin47 = db2_fetch_assoc($stmt47)) {
while($fin47 = db2_fetch_array($stmt47)) {
$fin47_results[] = $fin47;
}
$q48 = "SELECT DOKDT AS COMPDT, ORDNO AS ORDER, QTYOR AS OQTY, DKQTY AS RQTY, VNDNR AS VNDR FROM AMFLIBP.POHISTI WHERE ITNBR='$id' AND HOUSE='$wh'";
$stmt48 = db2_prepare($con, $q48);
$result48 = db2_execute($stmt48);
$fin48_results = array();
// while($fin48 = db2_fetch_assoc($stmt48)) {
while($fin48 = db2_fetch_array($stmt48)) {
$fin48_results[] = $fin48;
}
$array = array_merge($fin48_results, $fin48_results);
print_r($array);
如果您注意到我已添加 - 但是已注释掉db2_fetch_assoc
的号码,因为您的最终目标&原始数据集尚不清楚。但这应该有用。