PHP Returning Multiple Rows

2019-08-24 14:24发布

When I echo my query on screen and run directly in SSMS I get ONE row returned, which is exactly what I am after. When I run this query in my php I get multiple rows returned (seems to be one for each Dealer).

What should I change in this syntax so that I only have 1 row with totals displayed on screen in php like what happens in my SSMS? --> And if I copy/paste the results of the table into Excel to try and sort, all I have are 0's being displayed in the table, not actual data like what I see returned when I query directly in 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>";
}
}

EDIT
This is the echo statement that the above syntax produces that runs fine in 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 

EDIT 2
The below image shows what SSMS outputs on the top, and the bottom image is what my php outputs enter image description here

1条回答
Animai°情兽
2楼-- · 2019-08-24 15:03

$db->loadObjectList() is the Joomla function that will return a list of rows. If you want just one row you should use $db->loadObject() instead

查看更多
登录 后发表回答