PHP MySQL If Statement with IS NULL Example

Feb 04, 2022 . Admin

Hello Friends,

This article will give you example of PHP if statement with is null. I explained simply about PHP if statement with is null. This tutorial will give you simply example of PHP if statement with is null.

In this post, You'll learn use how to use if statement with is null in PHP. i will show you use of if statement with is null in PHP query.

Here i will give you many example for PHP if statement with is null.

Table: user_payments


So, let's see bellow solution:

index.php
<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db_php";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT 
            id, 
            user_id, 
            CASE WHEN charge IS NULL 
                THEN 0
                ELSE charge
            END as charge, 
            payment_date 
        FROM `user_payments`";

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
    echo '<pre>';
    print_r($row);
}

?>
Output:
Array
(
    [id] => 1
    [user_id] => 1
    [charge] => 50
    [payment_date] => 2022-02-02
)
Array
(
    [id] => 2
    [user_id] => 2
    [charge] => 45
    [payment_date] => 2022-02-02
)
Array
(
    [id] => 3
    [user_id] => 3
    [charge] => 0
    [payment_date] => 2022-02-02
)
Array
(
    [id] => 4
    [user_id] => 4
    [charge] => 10
    [payment_date] => 2022-02-02
)
It will help you...
#PHP