英文:
if column is null echoing in php getting wrong
问题
我正在使用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>
现在当我加载页面时,即使值存在于列中,它仍然显示"Vacant"。有人可以帮我解决我的代码吗?
英文:
I am creating a website in html using php and sql. there is a small section in which if the query didnt find the value, php will echo a message called vacant, but if the query finds the value in column, it will echo filled. the code is like below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- end snippet -->
Now when I load the page, even if the value is present in the column, its still showing "vacant". Can anyone please help me with my code?
答案1
得分: 4
请注意 ===
当使用 == 时,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";
}
?>
英文:
Note the ===
When use ==, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal
<?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";
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论