Cycle并不会显示SQL

时间:2018-11-14 10:08:20

标签: php sql-server

我的代码未显示查询在SQL中显示的所有结果,并且我不确定我为什么要有人帮助我找到问题,因此我在下面提供了以下代码: 我在php中查询(在同一个人的2张不同卡片的SQL中显示2个结果,但在php中仅显示一个结果)

$sql='SELECT Card.*
FROM Card
WHERE ValidUntil <= Cast(DateAdd( DD ,7 , GetDate() ) as Date)
ORDER BY ValidUntil ASC';
$stm = $conn->query($sql);

查询后我在php中的循环:

  
    

旧代码

if (!$stm)
{
    echo "";
}
else
{
    while ($rows2 = $stm->fetch())
    {
    $CType = isset($rows2['CardType']) ? $rows2['CardType'] : NULL;
    $CNr = isset($rows2['CardNr']) ? $rows2['CardNr'] : NULL;
    $CValFrom = isset($rows2['ValidFrom']) ? $rows2['ValidFrom'] : NULL;
    $CValUntil = isset($rows2['ValidUntil']) ? $rows2['ValidUntil'] : NULL;
    $CLTCCTime = isset($rows2['LastTccTime']) ? $rows2['LastTccTime'] : NULL;
    echo
  
     

'

';

     
    $tableHeaders = [
    ['variable' => 'CType', 'title' => 'Tipo'],
    ['variable' => 'CNr', 'title' => 'Nr.'],
    ['variable' => 'CValFrom', 'title' => 'Válido Desde'],
    ['variable' => 'CValUntil', 'title' => 'Válido Até'],
    ['variable' => 'CLTCCTime', 'title' => 'Dia/Hora no Último Terminal'],
    ];

    foreach($tableHeaders as $tableHeader):
        $variable = $tableHeader['variable'];
        if (isset($$variable) AND !is_null($$variable) AND !empty(trim($$variable))):
            echo '<th>' . $tableHeader['title'] . '</th>';
        endif;
    endforeach;

    echo '</tr></thead>';
echo '<tbody><tr>';

        $tableDatas1 = [
        ['variable1' => 'CType', 'data1' => $CType],
        ['variable1' => 'CNr', 'data1' => $CNr],
        ['variable1' => 'CValFrom', 'data1' => $mytime1],
        ['variable1' => 'CValUntil', 'data1' => $mytime2],
        ['variable1' => 'CLTCCTime', 'data1' => $mytime3],
        ];

        foreach($tableDatas1 as $tableData1):
            $variable1 = $tableData1['variable1'];
            if (isset($$variable1) AND !is_null($$variable1) AND !empty(trim($$variable1))):

                echo '<td class="text_body">' . $tableData1['data1'] . '</td>';
            endif;
        endforeach;

        echo '</tr></br>';
    }
}
echo '</tbody></table></body>';
  

在阅读注释之后,我开始更改代码,并在某个时候显示了它应有的所有结果,但是它为每一行显示了tableheader,而我只想要一个,并且结果在它下面,我进行了以下更改,但现在它仍然仅再次显示一个结果

if (!$stm)
{
    echo "";
}
else
{
    $rowsCabecalho = $stm->fetch();
    $CType = isset($rowsCabecalho['CardType']) ? $rowsCabecalho['CardType'] : NULL;
    $CNr = isset($rowsCabecalho['CardNr']) ? $rowsCabecalho['CardNr'] : NULL;
    $CValFrom = isset($rowsCabecalho['ValidFrom']) ? $rowsCabecalho['ValidFrom'] : NULL;
    $CValUntil = isset($rowsCabecalho['ValidUntil']) ? $rowsCabecalho['ValidUntil'] : NULL;
    $CLTCCTime = isset($rowsCabecalho['LastTccTime']) ? $rowsCabecalho['LastTccTime'] : NULL;
        $CCodingDate = isset($rowsCabecalho['CodingDate']) ? $rowsCabecalho['CodingDate'] : NULL;
    echo '<div class="informaltable"><p></p><table cellspacing="2" border="0"><thead><tr>';
    $tableHeaders = [
    ['variable' => 'CType', 'title' => 'Tipo'],
    ['variable' => 'CNr', 'title' => 'Nr.'],
    ['variable' => 'CValFrom', 'title' => 'Válido Desde'],
    ['variable' => 'CValUntil', 'title' => 'Válido Até'],
    ['variable' => 'CLTCCTime', 'title' => 'Dia/Hora no Último Terminal'],
    ];
    foreach($tableHeaders as $tableHeader):
        $variable = $tableHeader['variable'];
        if (isset($$variable) AND !is_null($$variable) AND !empty(trim($$variable))):
            echo '<th>' . $tableHeader['title'] . '</th>';
        endif;
    endforeach;
    echo '</tr></thead><tbody>';
}

if (!$stm)
{
    echo "";
}
else
{
    while ($rows2 = $stm->fetch())
    {
        $CType = isset($rows2['CardType']) ? $rows2['CardType'] : NULL;
        $CNr = isset($rows2['CardNr']) ? $rows2['CardNr'] : NULL;
        $CValFrom = isset($rows2['ValidFrom']) ? $rows2['ValidFrom'] : NULL;
        $CValUntil = isset($rows2['ValidUntil']) ? $rows2['ValidUntil'] : NULL;
        $CLTCCTime = isset($rows2['LastTccTime']) ? $rows2['LastTccTime'] : NULL;
        $CCodingDate = isset($rows2['CodingDate']) ? $rows2['CodingDate'] : NULL;

        echo '<tr>';
        $tableDatas1 = [
        ['variable1' => 'CType', 'data1' => $CType],
        ['variable1' => 'CNr', 'data1' => $CNr],
        ['variable1' => 'CValFrom', 'data1' => $mytime1],
        ['variable1' => 'CValUntil', 'data1' => $mytime2],
        ['variable1' => 'CLTCCTime', 'data1' => $mytime3],
        ];
        foreach($tableDatas1 as $tableData1):
            $variable1 = $tableData1['variable1'];
            if (isset($$variable1) AND !is_null($$variable1) AND !empty(trim($$variable1))):
                echo '<td class="text_body">' . $tableData1['data1'] . '</td>';
            endif;
        endforeach;
        echo '</tr>';
    }
}
echo '</tbody></table></body>';

1 个答案:

答案 0 :(得分:0)

解决方案:

从您的代码中删除$rowsCabecalho = $stm->fetch();。首先,您永远不会使用$rowsCabecalho,其次,不要两次致电$stm->fetch()

像这样使用PDOStatement::fetch

<?php
...
while ($row = $stmt->fetch()) {
   foreach($row => $value) {
      echo $value;
   }
   // your code ...
}
...

?>

更新

如果要先打印页眉,然后再打印行,则可以尝试以下操作:

<?php
echo '<div class="informaltable"><p></p><table cellspacing="2" border="0"><thead><tr>';
$tableHeaders = [
['variable' => 'CType', 'title' => 'Tipo'],
['variable' => 'CNr', 'title' => 'Nr.'],
['variable' => 'CValFrom', 'title' => 'Válido Desde'],
['variable' => 'CValUntil', 'title' => 'Válido Até'],
['variable' => 'CLTCCTime', 'title' => 'Dia/Hora no Último Terminal'],
];
foreach($tableHeaders as $tableHeader):
    $variable = $tableHeader['variable'];
    echo '<th>' . $tableHeader['title'] . '</th>';
endforeach;
echo '</tr></thead><tbody>';

if (!$stm)
{
    echo "";
}
else
{
    while ($rows2 = $stm->fetch())
    {
        $CType = isset($rows2['CardType']) ? $rows2['CardType'] : NULL;
        $CNr = isset($rows2['CardNr']) ? $rows2['CardNr'] : NULL;
        $CValFrom = isset($rows2['ValidFrom']) ? $rows2['ValidFrom'] : NULL;
        $CValUntil = isset($rows2['ValidUntil']) ? $rows2['ValidUntil'] : NULL;
        $CLTCCTime = isset($rows2['LastTccTime']) ? $rows2['LastTccTime'] : NULL;
        $CCodingDate = isset($rows2['CodingDate']) ? $rows2['CodingDate'] : NULL;

        echo '<tr>';
        $tableDatas1 = [
        ['variable1' => 'CType', 'data1' => $CType],
        ['variable1' => 'CNr', 'data1' => $CNr],
        ['variable1' => 'CValFrom', 'data1' => $mytime1],
        ['variable1' => 'CValUntil', 'data1' => $mytime2],
        ['variable1' => 'CLTCCTime', 'data1' => $mytime3],
        ];
        foreach($tableDatas1 as $tableData1):
            $variable1 = $tableData1['variable1'];
            if (isset($$variable1) AND !is_null($$variable1) AND !empty(trim($$variable1))):
                echo '<td class="text_body">' . $tableData1['data1'] . '</td>';
            endif;
        endforeach;
        echo '</tr>';
    }
}
echo '</tbody></table></body>';
?>