我正在使用 php 和 sql 创建一个 html 网站。有一小部分,如果查询没有找到该值,php 将回显一条名为 vacant 的消息,但如果查询在列中找到该值,它将回显filled。代码如下:
<div style="padding-bottom: 10px;" class="col">
<?php
$query=mysqli_query($con,"select * from king where title= 'SIMS'");
$sep=mysqli_fetch_array($query);
$c1 = $sep['title'];
if ($c1 == NULL){
$msg = "Vacant";
}
else {
$msg = "Filled";
}
?>
<div class="counter col_fourth">
<h2 class="timer count-title count-number" data-to="300" data-speed="1500"></h2>
<p class="count-text "> <?php echo $msg;?> </p>
<p class="count-text ">title</p>
</div>
</div>
现在,当我加载页面时,即使该值存在于列中,它仍然显示“空白”。谁能帮我修改我的代码吗?
最佳答案
注意===
当使用 == 时,就像你所做的那样,PHP 将 NULL、false、0、空字符串和空数组视为相等
<?php
$query=mysqli_query($con,"select * from king where title= 'SIMS'");
$sep=mysqli_fetch_array($query);
$c1 = $sep['title'];
if ($c1 === NULL){
$msg = "Vacant";
}
else {
$msg = "Filled";
}
?>
关于php - 如果列为空,在 php 中回显会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608673/