if column is null echoing in php getting wrong

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

if column is null echoing in php getting wrong

问题

我正在使用PHP和SQL创建一个HTML网站。有一个小部分,如果查询没有找到值,PHP将输出一个名为"Vacant"的消息,但如果查询在列中找到值,它将输出"Filled"。代码如下:

  1. <div style="padding-bottom: 10px;" class="col">
  2. <?php
  3. $query=mysqli_query($con,"select * from king where title= 'SIMS'");
  4. $sep=mysqli_fetch_array($query);
  5. $c1 = $sep['title'];
  6. if ($c1 == NULL){
  7. $msg = "Vacant";
  8. }
  9. else {
  10. $msg = "Filled";
  11. }
  12. ?>
  13. <div class="counter col_fourth">
  14. <h2 class="timer count-title count-number" data-to="300" data-speed="1500"></h2>
  15. <p class="count-text "> <?php echo $msg;?> </p>
  16. <p class="count-text ">title</p>
  17. </div>
  18. </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 -->

  1. &lt;div style=&quot;padding-bottom: 10px;&quot; class=&quot;col&quot;&gt;
  2. &lt;?php
  3. $query=mysqli_query($con,&quot;select * from king where title= &#39;SIMS&#39;&quot;);
  4. $sep=mysqli_fetch_array($query);
  5. $c1 = $sep[&#39;title&#39;];
  6. if ($c1 == NULL){
  7. $msg = &quot;Vacant&quot;;
  8. }
  9. else {
  10. $msg = &quot;Filled&quot;;
  11. }
  12. ?&gt;
  13. &lt;div class=&quot;counter col_fourth&quot;&gt;
  14. &lt;h2 class=&quot;timer count-title count-number&quot; data-to=&quot;300&quot; data-speed=&quot;1500&quot;&gt;&lt;/h2&gt;
  15. &lt;p class=&quot;count-text &quot;&gt; &lt;?php echo $msg;?&gt; &lt;/p&gt;
  16. &lt;p class=&quot;count-text &quot;&gt;title&lt;/p&gt;
  17. &lt;/div&gt;
  18. &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、空字符串和空数组视为相等

  1. <?php
  2. $query=mysqli_query($con,"select * from king where title= 'SIMS'");
  3. $sep=mysqli_fetch_array($query);
  4. $c1 = $sep['title'];
  5. if ($c1 === NULL){
  6. $msg = "Vacant";
  7. }
  8. else {
  9. $msg = "Filled";
  10. }
  11. ?>
英文:

Note the ===

When use ==, as you did, PHP treats NULL, false, 0, the empty string, and empty arrays as equal

  1. &lt;?php
  2. $query=mysqli_query($con,&quot;select * from king where title= &#39;SIMS&#39;&quot;);
  3. $sep=mysqli_fetch_array($query);
  4. $c1 = $sep[&#39;title&#39;];
  5. if ($c1 === NULL){
  6. $msg = &quot;Vacant&quot;;
  7. }
  8. else {
  9. $msg = &quot;Filled&quot;;
  10. }
  11. ?&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:

确定