[英]Find null values returned by SQL query
我正在调用一个从视图中选择所有内容的 SQL 语句。 可能返回了一些空值,我想找到它们并在 HTML 文档中突出显示它们。 到目前为止,这是我的代码。 如何找到空列(可以在图片中看到)以便我可以用 CSS 突出显示它们?
谢谢。
<?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';
?>
如果我理解正确,您正在寻找这样的东西:
<?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; ?>
请注意,此代码容易受到 XSS 的攻击,因为您在回显时没有对数据进行转义。 我建议仅在本地使用此代码进行学习。
互联网上有一些优秀的文章,您可以阅读以了解如何防止 XSS 注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.