MySQL Query有2个数组,但其中1个没有显示结果

时间:2015-01-13 07:04:45

标签: php mysql

我的代码如下

<?php

mysql_connect("x", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());

$query = "select sp1.sale_id idsale, 
round((sik1.quantity_purchased*sik1.item_unit_price)+((IFNULL(0,sit1.percent)/100)*(sik1.quantity_purchased*sik1.item_unit_price))-
((sik1.discount_percent/100)*(sik1.quantity_purchased*sik1.item_unit_price))) as total,
round(sum(sp1.payment_amount)) as paid, DATE_FORMAT(now(),'%Y-%m-%d') as now, 
DATE_FORMAT(DATE_ADD(max(sp1.payment_date),INTERVAL 20 DAY),'%Y-%m-%d') as 'duedate',

if ((round((sik1.quantity_purchased*sik1.item_unit_price)+((IFNULL(0,sit1.percent)/100)*(sik1.quantity_purchased*sik1.item_unit_price))-
((sik1.discount_percent/100)*(sik1.quantity_purchased*sik1.item_unit_price))))>(round(sum(sp1.payment_amount))),0,1) as lunas,
if ((DATEDIFF(DATE_FORMAT(DATE_ADD(max(sp1.payment_date),INTERVAL 10 DAY),'%Y-%m-%d'),DATE_FORMAT(now(),'%Y-%m-%d')))<0,0,1) due

from sales_payments sp1
left join sales_items sik1 on (sik1.sale_id=sp1.sale_id)
left join sales_items_taxes sit1 on
(sit1.sale_id=sp1.sale_id)
where sik1.quantity_purchased IS NOT NULL
group by sp1.sale_id
union
select sp2.sale_id idsale, 
round((sik2.quantity_purchased*sik2.item_kit_unit_price)+((IFNULL(0,sit2.percent)/100)*(sik2.quantity_purchased*sik2.item_kit_unit_price))-
((sik2.discount_percent/100)*(sik2.quantity_purchased*sik2.item_kit_unit_price))) as total,
round(sum(sp2.payment_amount)) as paid, DATE_FORMAT(now(),'%Y-%m-%d') as now, 
DATE_FORMAT(DATE_ADD(max(sp2.payment_date),INTERVAL 20 DAY),'%Y-%m-%d') as 'duedate',

if ((round((sik2.quantity_purchased*sik2.item_kit_unit_price)+((IFNULL(0,sit2.percent)/100)*(sik2.quantity_purchased*sik2.item_kit_unit_price))-
((sik2.discount_percent/100)*(sik2.quantity_purchased*sik2.item_kit_unit_price))))>(round(sum(sp2.payment_amount))),0,1) as lunas,
if ((DATEDIFF(DATE_FORMAT(DATE_ADD(max(sp2.payment_date),INTERVAL 10 DAY),'%Y-%m-%d'),DATE_FORMAT(now(),'%Y-%m-%d')))<0,0,1) due

from sales_payments sp2
left join sales_item_kits sik2 on (sik2.sale_id=sp2.sale_id)
left join sales_item_kits_taxes sit2 on
(sit2.sale_id=sp2.sale_id)
where sik2.quantity_purchased IS NOT NULL
group by sp2.sale_id";

/*
The result of query is

"payment_id";"sale_id";"payment_type";"payment_amount";"truncated_card";"card_issuer";"payment_date"
"84";"49";"Tunai";"150000.0000000000";;;"2015-01-11 00:01:40"
"85";"52";"Tunai";"150000.0000000000";"0";;"2015-01-11 00:18:41"
"87";"53";"Tunai";"750000.0000000000";"0";;"2015-01-11 00:24:53"
"102";"50";"Tunai";"75000.0000000000";"0";;"2014-12-16 22:45:12"
"104";"54";"Tunai";"20000.0000000000";"0";;"2015-01-12 02:36:19"
"105";"51";"Tunai";"135000.0000000000";"0";;"2015-01-13 03:04:49"

*/
$result = mysql_query($query) or die ("no query");
            $result_array = array();

            while($ngebon = mysql_fetch_assoc($result))
            {
                if ($ngebon['total'] > $ngebon['paid']) { 
                    $result_array[] = $ngebon['idsale'];    
                    echo $ngebon['idsale']." ".$ngebon['due']."<br>"; //result is idsale 50 and 51
                }
            }

            $result2 = mysql_query($query) or die ("no query");
            $result_array_due = array();
            while($ngebon2 = mysql_fetch_assoc($result2))
            { 
                if ($ngebon2['due'] = 0) { 
                    $result_array_due[] = $ngebon2['idsale'];
                    echo $ngebon2['idsale']." ".$ngebon2['due']."<br>"; //no result
                }
            }
?>

我上面的代码有2个数组,但访问相同的查询,但请看数组的名称是不同的。

我期待的是展示

$ngebon2['idsale'] where $ngebon2['due'] = 0 (Column 'due' is int).

但我所做的一切都没有结果$ ngebon2而$ ngebon正确显示。事实是在phpmyadmin中执行查询显示有1个结果

of$ngebon2['idsale'] where $ngebon2['due'] = 0  . 

您可以查看上面的查询结果。

有人可以帮忙吗?

0 个答案:

没有答案