简体   繁体   English

查找 SQL 查询返回的空值

[英]Find null values returned by SQL query

I am calling an SQL statement which selects everything from a view.我正在调用一个从视图中选择所有内容的 SQL 语句。 There might be some null values returned, I would like to find them and highlight them in the HTML document.可能返回了一些空值,我想找到它们并在 HTML 文档中突出显示它们。 This is my code so far.到目前为止,这是我的代码。 How can I find the empty columns (which can be seen in the picture) so I could highlight them with CSS?如何找到空列(可以在图片中看到)以便我可以用 CSS 突出显示它们?

Thanks.谢谢。


<?php 
require_once '../includes/header.php'; 

$sql = $conn->prepare("SELECT * FROM vw_allpropertiesandagents ORDER BY Price");
$sql->execute();
?>

<section class="main-sec">
<h1>All Agents and All properties</h1>
<p>The properties even the ones that have no agents assigned to them are displayed</p>
<table class ='dba-table'>
    <tr>
        <th>Property Id</th>
        <th>Full Address</th>
        <th>Price</th>
        <th>Property Agent Id</th>
        <th>Agent Id</th>
        <th>For</th>
        <th>Property Type</th>
    </tr>
<?php while ($row = $sql->fetch()){ ?>
    <tr>
    <?php 

    foreach ($row as $value){ ?>
        <td><?php echo $value?></td>
    <?php } ?>
    </tr>


    <?php } ?>
</table>
</section>


<?php 
require_once '../includes/footer.php'; 

?>

This is the HTML output ] 1这是 HTML 输出] 1

If I understand correctly you are looking for something like this:如果我理解正确,您正在寻找这样的东西:

<?php foreach ($row as $value): ?>
   <?php if($value === null): ?>
      <td style="background-color: red;">Empty</td>
   <?php else: ?>
      <td><?= $value ?></td>
   <?php endif; ?>
<?php endforeach; ?>

Please be aware that this code is vulnerable to XSS because you are not escaping the data when echo'ing.请注意,此代码容易受到 XSS 的攻击,因为您在回显时没有对数据进行转义。 I would recommend to only use this code locally for learning purposes.我建议仅在本地使用此代码进行学习。

There are some excellent articles on the internet, which you can read to learn how to prevent XSS injection.互联网上有一些优秀的文章,您可以阅读以了解如何防止 XSS 注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM