查询在SQL Manager中有效,但不在PHP

时间:2016-12-09 14:06:47

标签: php sql-server

我使用此查询来计算来自两个表的票证。

SELECT t1.institucion,
       SUM(CASE WHEN t1.fecha LIKE '2016-07%' THEN 1 ELSE 0 END) AS July,
       SUM(CASE WHEN t1.fecha LIKE '2016-08%' THEN 1 ELSE 0 END) AS August,
       SUM(CASE WHEN t1.fecha LIKE '2016-09%' THEN 1 ELSE 0 END) AS September,
       SUM(CASE WHEN t1.fecha LIKE '2016-10%' THEN 1 ELSE 0 END) AS October,
       SUM(CASE WHEN t1.fecha LIKE '2016-11%' THEN 1 ELSE 0 END) AS November,
       SUM(CASE WHEN t1.fecha LIKE '2016-12%' THEN 1 ELSE 0 END) AS December,
       SUM(CASE WHEN t1.fecha LIKE '2017-01%' THEN 1 ELSE 0 END) AS January,
       SUM(CASE WHEN t1.fecha LIKE '2017-02%' THEN 1 ELSE 0 END) AS February,
       SUM(CASE WHEN t1.fecha LIKE '2017-03%' THEN 1 ELSE 0 END) AS March,
       SUM(CASE WHEN t1.fecha LIKE '2017-04%' THEN 1 ELSE 0 END) AS April,
       SUM(CASE WHEN t1.fecha LIKE '2017-05%' THEN 1 ELSE 0 END) AS May,
       SUM(CASE WHEN t1.fecha LIKE '2017-06%' THEN 1 ELSE 0 END) AS June
FROM
(
    SELECT institucion, fecha 
    FROM uploader n
    UNION ALL
    SELECT institucion, fecha 
    FROM uploaderedusystem s
) t1
INNER JOIN data t2
    ON t1.institucion = t2.institucion

WHERE t2.erate = 'y'
group by t1.institucion;

我需要在t1中添加最后创建的故障单,因此,我将查询编辑为:

SELECT t1.institucion, max(t1.fecha) AS fecha ,
       SUM(CASE WHEN t1.fecha LIKE '2016-07%' THEN 1 ELSE 0 END) AS July,
       SUM(CASE WHEN t1.fecha LIKE '2016-08%' THEN 1 ELSE 0 END) AS August,
       SUM(CASE WHEN t1.fecha LIKE '2016-09%' THEN 1 ELSE 0 END) AS September,
       SUM(CASE WHEN t1.fecha LIKE '2016-10%' THEN 1 ELSE 0 END) AS October,
       SUM(CASE WHEN t1.fecha LIKE '2016-11%' THEN 1 ELSE 0 END) AS November,
       SUM(CASE WHEN t1.fecha LIKE '2016-12%' THEN 1 ELSE 0 END) AS December,
       SUM(CASE WHEN t1.fecha LIKE '2017-01%' THEN 1 ELSE 0 END) AS January,
       SUM(CASE WHEN t1.fecha LIKE '2017-02%' THEN 1 ELSE 0 END) AS February,
       SUM(CASE WHEN t1.fecha LIKE '2017-03%' THEN 1 ELSE 0 END) AS March,
       SUM(CASE WHEN t1.fecha LIKE '2017-04%' THEN 1 ELSE 0 END) AS April,
       SUM(CASE WHEN t1.fecha LIKE '2017-05%' THEN 1 ELSE 0 END) AS May,
       SUM(CASE WHEN t1.fecha LIKE '2017-06%' THEN 1 ELSE 0 END) AS June
FROM
(
    SELECT institucion, fecha 
    FROM uploader n
    where fecha <> '2017-07-31'
    UNION ALL
    SELECT institucion, fecha 
    FROM uploaderedusystem s
    where fecha <> '2017-07-31'
) t1
INNER JOIN data t2
    ON t1.institucion = t2.institucion

WHERE t2.erate = 'y'
group by t1.institucion;

查询在SQL Server Manager中运行得非常好,但是当我从php运行它时,当回显添加的列时,表总是空白。

这是我的PHP代码:

$currentyear = date(Y);
$nextyear = date(Y) +1;
$uploaderquery = "SELECT t1.institucion, max(t1.fecha) as fecha,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-07%' THEN 1 ELSE 0 END) AS July,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-08%' THEN 1 ELSE 0 END) AS August,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-09%' THEN 1 ELSE 0 END) AS September,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-10%' THEN 1 ELSE 0 END) AS October,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-11%' THEN 1 ELSE 0 END) AS November,
       SUM(CASE WHEN t1.fecha LIKE '$currentyear-12%' THEN 1 ELSE 0 END) AS December,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-01%' THEN 1 ELSE 0 END) AS January,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-02%' THEN 1 ELSE 0 END) AS February,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-03%' THEN 1 ELSE 0 END) AS March,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-04%' THEN 1 ELSE 0 END) AS April,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-05%' THEN 1 ELSE 0 END) AS May,
       SUM(CASE WHEN t1.fecha LIKE '$nextyear-06%' THEN 1 ELSE 0 END) AS June
FROM
(
    SELECT institucion, fecha 
    FROM uploader n

    UNION ALL
    SELECT institucion, fecha 
    FROM uploaderedusystem s

) t1
INNER JOIN data t2
    ON t1.institucion = t2.institucion
WHERE t2.erate = 'y'
group by t1.institucion;";
  $tickets = sqlsrv_query($conn, $uploaderquery);
 mysqli_close($conn);


 echo '<table class="table table-striped table-bordered round">
  <tr style="background-color:#84C600">
     <th>Institution</th>
     <th>July</th>   
     <th>August</th>
     <th>September</th>  
     <th>October</th>
     <th>November</th>   
     <th>December</th>   
     <th>January</th>    
     <th>February</th>   
     <th>March</th>  
     <th>April</th>  
     <th>May</th>    
     <th>June</th>

     <th>Total</th>
     </tr>' ;
while ($record = sqlsrv_fetch_array($tickets)){
    $status_colors = array(0 => '#FF9999');
    $status_colors2 = array(0 => 'red');
    $july = $record['July'];
    $august = $record['August'];
    $september = $record['September'];
    $october = $record['October'];
    $november = $record['November'];
    $december = $record['December'];
    $january = $record['January'];
    $february = $record['February'];
    $march = $record['March'];
    $april = $record['April'];
    $may = $record['May'];
    $june = $record['June'];
    $lastvisit = $record['fecha'];

    $alert = $july + $august + $september + $october + $november + $december + $january + $february + $march + $april + $may + $june;

    $icon = '';
     if ($alert == 0){
         $icon = '<i style="color:red" class="fa fa-exclamation" aria-hidden="true"></i>    ';
     };

     echo '
  <tr>
     <td>'.$icon.'   '.$record['institucion'] .'</td>
     <td style="color:'.$status_colors[$july].'">'.$record['July'].' </td>
     <td style="color:'.$status_colors[$august].'">'.$record['August'].' </td>
     <td style="color:'.$status_colors[$september].'">'.$record['September'].' </td>
     <td style="color:'.$status_colors[$october].'">'.$record['October'].' </td>
     <td style="color:'.$status_colors[$november].'">'.$record['November'].' </td>
     <td style="color:'.$status_colors[$december].'">'.$record['December'].' </td>
     <td style="color:'.$status_colors[$january].'">'.$record['January'].' </td>
     <td style="color:'.$status_colors[$february].'">'.$record['February'].' </td>
     <td style="color:'.$status_colors[$march].'">'.$record['March'].' </td>
     <td style="color:'.$status_colors[$april].'">'.$record['April'].' </td>
     <td style="color:'.$status_colors[$may].'">'.$record['May'].' </td>
     <td style="color:'.$status_colors[$june].'">'.$record['June'].' </td>
     <td style="color:'.$status_colors2[$alert].'">'.$alert.'</td>
     <td style="color:'.$status_colors2[$alert].'">'.$record['fecha'].'</td>
     </tr>';    

 }
 echo '</table>';

如果我删除行<td style="color:'.$status_colors2[$alert].'">'.$record['fecha'].'</td>,一切都会完美运行。

0 个答案:

没有答案