代码之家  ›  专栏  ›  技术社区  ›  cloudseeker

未找到记录时不显示消息

  •  1
  • cloudseeker  · 技术社区  · 8 年前

    出于某种原因,以下内容在查询数据库中的选定范围时没有显示“未找到结果”错误消息,而是显示了表头和页脚。

        $result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
    
    <?php
    
    if (!$result) { echo("No results found for the selected view");
              } else ?>
              <table id="results">
    <tr>
    <th>Rec#</th>
    <th>Date</th>
    <th>Time</th>
    <th>Reading</th>
    </tr>
    <?php ;
    while($row = mysqli_fetch_array($result)) 
    ?>
    <tr>
    
    <td><?php echo($row['RecID']);?></td>
    <td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
    <td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
    <td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td class="footer" colspan="4">- end of report -</td></tr>
    </table>
    <?php
    mysqli_close($con);
    ?>
    

    任何援助都将不胜感激,因为从理论上讲,这应该是可行的。。。不是吗?:-)

    2 回复  |  直到 8 年前
        1
  •  3
  •   Jakumi    8 年前

    $result 可能是结果集,但可能为空。然而 !$result mysql_query :

    退换商品 FALSE 失败时。为了成功 SELECT SHOW , DESCRIBE EXPLAIN 查询 mysqli_query() mysqli_result 对象对于其他成功的查询 mysqli_query() TRUE http://php.net/mysqli_query )

    你应该向 mysqli_num_rows

        2
  •  1
  •   Moeen Basra    8 年前

    可以使用此条件检查查询中的行数

    $result = mysqli_query($con,"SELECT * FROM tblRecords WHERE DATE(RecDate) = CURDATE() - INTERVAL 1 DAY ORDER BY RecDate DESC, RecTime DESC");
    
    <?php
    
    if (mysqli_num_rows($result) === 0) { 
        echo("No results found for the selected view");
    } else {?>
    <table id="results">
    <tr>
    <th>Rec#</th>
    <th>Date</th>
    <th>Time</th>
    <th>Reading</th>
    </tr>
    <?php ;
    while($row = mysqli_fetch_array($result)) 
    ?>
    <tr>
    
    <td><?php echo($row['RecID']);?></td>
    <td><?php echo(date("d/m/Y", strtotime($row['RecDate'])));?></td>
    <td><?php echo(date("g:i A", strtotime($row['RecTime'])));?></td>
    <td><?php echo($row['RecReading'] . $row['RecMeasure']);?></td>
    </tr>
    <?php } ?>
    <tr>
    <td class="footer" colspan="4">- end of report -</td></tr>
    </table>
    <?php
    mysqli_close($con);
    ?>