PHP返回多行

时间:2017-06-26 14:33:38

标签: php html joomla html-table joomla3.0

当我在屏幕上回显我的查询并直接在SSMS中运行时,我返回了一行,这正是我所追求的。当我在我的php中运行此查询时,我得到多行返回(似乎是每个经销商的一行)。

我应该在这个语法中做些什么改变,以便我只有1行显示在屏幕上的总数,就像在我的SSMS中发生的那样? - >如果我将表的结果复制/粘贴到Excel中进行尝试和排序,我在表中显示的是0,而不是像我在SSMS中直接查询时看到的那样的实际数据

    {
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$Dealer = $_POST['dealer'];

$option = array();
$option['driver'] = 'mssql';
$option['host'] = '192.5.100.23';
$option['user'] = 'username';
$option['password'] = 'passs';
$option['database'] = 'test';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query11 = $db->getquery11(true);
$query11 = "SELECT
MAX(A.Value1) AS Value1,
MAX(A.Value10) AS WorkinIT,
MAX(A.Value11) AS OTG
FROM
(
SELECT
ZT1.Dealer,
SUM(ISNULL(ZT1.[Value1],0)) AS Value1,
0 AS Value10,
0 AS Value11
FROM ROCK AS ZT1
WHERE ZT1.[PUD] >= '$startdate'
AND ZT1.[PUD] <= '$enddate'";
if ($Dealer != 'All') {
$query11 .= " AND ZT1.Dealer = '$Dealer'";
}
$query11 .= " GROUP BY ZT1.Dealer
UNION ALL
SELECT
ZT2.Dealer,
0 As Value1,
0 AS Value10,
0 AS Value11
FROM GALL ZT2
WHERE ZT2.[sellD] >= '$startdate'
AND ZT2.[sellD] <= '$enddate'";
if ($Dealer != 'All') {
$query11 .= " AND ZT2.Dealer = '$Dealer'";
}
$query11 .= " GROUP BY ZT2.Dealer
UNION ALL
SELECT
Dealer = RTRIM(LTRIM(dealer)),
0 As Value1,
SUM(ISNULL(abc,0)) AS Value10,
0 AS Value11
FROM WorkinIT AS C1
GROUP BY dealer
UNION ALL
SELECT
Dealer = RTRIM(LTRIM(dealer)),
0 As Value1,
0 AS Value10,
SUM(ISNULL(prs,0)) AS Value11
FROM eOTG AS C2";
if ($Dealer != 'All') {
$query11 .= " WHERE C2.Dealer = '$Dealer'";
}

$query11 .= " GROUP BY dealer) AS A";
}

echo $query11;
$db->setquery11($query11);
$query11 = $db->loadObjectList();
if ($query11) 
{
?>
<table border="1">
<thead>
<tr>
<th>Value </th>
<th>OTG </th>
<th>WorkinIT </th>
</tr>
</thead>
<?php
foreach ($query11 as $res) 
{
print "<tr>";
print "<td>" . "$" . round($res->Value1) . "</td>";
print "<td>" . "$" . round($res->WorkinIT) . "</td>";
print "<td>" . "$" . round($res->OTG) . "</td>";
print "</tr>";
}
}

修改
这是上面语法生成的echo语句,可以在SSMS GUI中正常运行

SELECT Max(A.value1)  AS Value1, 
       Max(A.value10) AS WorkinIT, 
       Max(A.value11) AS OTG 
FROM   (SELECT ZT1.dealer, 
               Sum(Isnull(ZT1.[value1], 0)) AS Value1, 
               0                            AS Value10, 
               0                            AS Value11 
        FROM   rock AS ZT1 
        WHERE  ZT1.[pud] >= '2017-06-01' 
               AND ZT1.[pud] <= '2017-06-22' 
        GROUP  BY ZT1.dealer 
        UNION ALL 
        SELECT ZT2.dealer, 
               0 AS Value1, 
               0 AS Value10, 
               0 AS Value11 
        FROM   gall ZT2 
        WHERE  ZT2.[selld] >= '2017-06-01' 
               AND ZT2.[selld] <= '2017-06-22' 
        GROUP  BY ZT2.dealer 
        UNION ALL 
        SELECT dealer = Rtrim(Ltrim(dealer)), 
               0                   AS Value1, 
               Sum(Isnull(abc, 0)) AS Value10, 
               0                   AS Value11 
        FROM   workinit AS C1 
        GROUP  BY dealer 
        UNION ALL 
        SELECT dealer = Rtrim(Ltrim(dealer)), 
               0                   AS Value1, 
               0                   AS Value10, 
               Sum(Isnull(prs, 0)) AS Value11 
        FROM   eotg AS C2 
        GROUP  BY dealer) AS A 

编辑2
下图显示了SSMS在顶部输出的内容,而底部图像是我的php输出的内容 enter image description here

1 个答案:

答案 0 :(得分:0)

$ db-&gt; loadObjectList()是将返回行列表的Joomla函数。 如果只想要一行,则应使用$ db-&gt; loadObject()代替