Skip to content
Advertisement

if column is null echoing in php getting wrong

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:

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

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?

Advertisement

Answer

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";
}    
?>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement