if column is null echoing in php getting wrong

huangapple go评论70阅读模式
英文:

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 -->

&lt;div style=&quot;padding-bottom: 10px;&quot; class=&quot;col&quot;&gt;
    &lt;?php
    $query=mysqli_query($con,&quot;select * from king where title= &#39;SIMS&#39;&quot;);
    $sep=mysqli_fetch_array($query);
    $c1 = $sep[&#39;title&#39;];
    
    if ($c1 == NULL){    
        $msg = &quot;Vacant&quot;;
    }
    else {
        $msg = &quot;Filled&quot;;
    }    
    ?&gt;
  &lt;div class=&quot;counter col_fourth&quot;&gt;
    &lt;h2 class=&quot;timer count-title count-number&quot; data-to=&quot;300&quot; data-speed=&quot;1500&quot;&gt;&lt;/h2&gt;
    &lt;p class=&quot;count-text &quot;&gt; &lt;?php echo $msg;?&gt; &lt;/p&gt;
    &lt;p class=&quot;count-text &quot;&gt;title&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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

&lt;?php
$query=mysqli_query($con,&quot;select * from king where title= &#39;SIMS&#39;&quot;);
$sep=mysqli_fetch_array($query);
$c1 = $sep[&#39;title&#39;];

if ($c1 === NULL){    
    $msg = &quot;Vacant&quot;;
}
else {
    $msg = &quot;Filled&quot;;
}    
?&gt;

huangapple
  • 本文由 发表于 2020年1月6日 16:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定