不止一个人选择'查询中的语句

时间:2016-09-13 14:17:47

标签: php sql

在PHP返回结果行为方面遇到一些困难,似乎SQL语句中有多个select语句没有返回结果。 (但在管理工作室执行时工作正常)

如果我评论查询的部分给我扩展信息(前两个选择到临时表)和最后一个选择的连接,那么查询工作正常,所以我把它缩小到一个问题有多个选择。

所以这段代码中提到的部分已经注释掉了。

foreach ($_POST['BeginLocation'] as $key => $value) {$LocationID = $key;}

echo '<div class="nmform"><form name="StockTake" action="index.php" method="post" enctype="multipart/form-data">';

include "../DBCon/RDPNearMisses.php";

$GetProducts = sqlsrv_query($NMDB, "/*select ProductID, max(StockTakeDate) as 'NewestStk'
                                    into #temp1
                                    from [JobObservations].[dbo].[ITStk.StockTake]
                                    where StockTakeDate > DATEADD(d, -14, GETDATE())
                                    group by ProductID

                                    select ST.ProductID, ST.StockTakeDate, ST.Qty
                                    into #temp2
                                    from [JobObservations].[dbo].[ITStk.StockTake] ST
                                    inner join #temp1 t1 on ST.ProductID = T1.ProductID and ST.StockTakeDate = T1.NewestStk

                                    --drop table #temp1*/

                                    select PG.GroupName, PD.ProductID, PD.ProductGroupID, PD.ProductCode, PD.ProductDescription--, t2.Qty as [LastQty]
                                    from [JobObservations].[dbo].[ITStk.Products] as PD
                                    inner join [JobObservations].[dbo].[ITStk.ProductGroups] as PG on PD.ProductGroupID = PG.ProductGroupID
                                    inner join [JobObservations].[dbo].[ITStk.Locations] as LC on PD.LocationID = LC.LocationID
                                    --left join #temp2 t2 on PD.ProductID = t2.ProductID
                                    where PD.LocationID = $LocationID
                                    order by LC.LocationDescription asc, PG.GroupName asc, PD.ProductCode asc

                                    --drop table #temp2");

if( $GetProducts === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
}
echo '<table class="questiontable" style="width: 600px;"><th style=width: 50%;>Code</th><th>Description</th><th style="width: 50px;">Qty</th><th>LastQty</th>';
while ($row1= sqlsrv_fetch_array($GetProducts))
{
         if ($ProductGroupCheck != $row1['ProductGroupID']) {echo '<tr><td style="font-weight: bold; font-size: larger;" colspan="3";>'.$row1['GroupName'].'</td></tr>';}
         echo '<tr class="rowb"><td>'.$row1['ProductCode'].'</td><td>'.$row1['ProductDescription'].'</td><td><select class="styled-select" name="StockArray['.$row1['ProductID'].']" required><option selected></option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option></select></td><td>'.$row1['LastQty'].'</td></tr>';
         $ProductGroupCheck = $row1['ProductGroupID'];
}
echo '</table>';

如果SQL被修改为低于该查询,则返回无结果(并且不会从检查中抛出SQL错误)

"select ProductID, max(StockTakeDate) as 'NewestStk'
                                    into #temp1
                                    from [JobObservations].[dbo].[ITStk.StockTake]
                                    where StockTakeDate > DATEADD(d, -14, GETDATE())
                                    group by ProductID

                                    select ST.ProductID, ST.StockTakeDate, ST.Qty
                                    into #temp2
                                    from [JobObservations].[dbo].[ITStk.StockTake] ST
                                    inner join #temp1 t1 on ST.ProductID = T1.ProductID and ST.StockTakeDate = T1.NewestStk

                                    drop table #temp1

                                    select PG.GroupName, PD.ProductID, PD.ProductGroupID, PD.ProductCode, PD.ProductDescription, t2.Qty as [LastQty]
                                    from [JobObservations].[dbo].[ITStk.Products] as PD
                                    inner join [JobObservations].[dbo].[ITStk.ProductGroups] as PG on PD.ProductGroupID = PG.ProductGroupID
                                    inner join [JobObservations].[dbo].[ITStk.Locations] as LC on PD.LocationID = LC.LocationID
                                    left join #temp2 t2 on PD.ProductID = t2.ProductID
                                    where PD.LocationID = $LocationID
                                    order by LC.LocationDescription asc, PG.GroupName asc, PD.ProductCode asc

                                    drop table #temp2");

我也尝试将其更改为存储过程并在PHP中执行它,行为是相同的。

请忽略我在代码中有变量的事实,当我开始工作时,我会把它写成一个准备好的语句。

0 个答案:

没有答案