MySQLi Tutorial on MySQLi Handling NULL Values

we have seen the sql select command along with the where clause to fetch data from a mysql table, but when we try to give a condition, which compares the field or the column value to null, it does not work properly.

to handle such a situation, mysql provides three operators −

  • is null − this operator returns true, if the column value is null.

  • is not null − this operator returns true, if the column value is not null.

  • <=> − this operator compares values, which (unlike the = operator) is true even for two null values.

the conditions involving null are special. you cannot use = null or != null to look for null values in columns. such comparisons always fail because it is impossible to tell whether they are true or not. sometimes, even null = null fails.

to look for columns that are or are not null, use is null or is not null.

using null values at the command prompt

assume that there is a table called tcount_tbl in the tutorials database and it contains two columns namely tutorial_author and tutorial_count, where a null tutorial_count indicates that the value is unknown.

example

try the following examples −

root@host# mysql -u root -p password;
enter password:*******

mysql> use tutorials;
database changed

mysql> create table tcount_tbl
   → (
   → tutorial_author varchar(40) not null,
   → tutorial_count  int
   → );
query ok, 0 rows affected (0.05 sec)

mysql> insert into tcount_tbl
   → (tutorial_author, tutorial_count) values ('mahran', 20);

mysql> insert into tcount_tbl
   → (tutorial_author, tutorial_count) values ('mahnaz', null);

mysql> insert into tcount_tbl
   → (tutorial_author, tutorial_count) values ('jen', null);

mysql> insert into tcount_tbl
   → (tutorial_author, tutorial_count) values ('gill', 20);

mysql> select * from tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
|     mahran      |       20       |
|     mahnaz      |      null      |
|      jen        |      null      |
|     gill        |       20       |
+-----------------+----------------+
4 rows in set (0.00 sec)

mysql>

you can see that = and != do not work with null values as follows −

mysql> select * from tcount_tbl where tutorial_count = null;
empty set (0.00 sec)

mysql> select * from tcount_tbl where tutorial_count != null;
empty set (0.01 sec)

to find the records where the tutorial_count column is or is not null, the queries should be written as shown in the following program.

mysql> select * from tcount_tbl 
   → where tutorial_count is null;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
|     mahnaz      |      null      |
|      jen        |      null      |
+-----------------+----------------+
2 rows in set (0.00 sec)
mysql> select * from tcount_tbl 
   → where tutorial_count is not null;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
|     mahran      |       20       |
|     gill        |       20       |
+-----------------+----------------+
2 rows in set (0.00 sec)

handling null values in a php script

you can use the if...else condition to prepare a query based on the null value.

the following example takes the tutorial_count from outside and then compares it with the value available in the table.

example

copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>handling null</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'tutorials';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         $tutorial_count = null;
         if($mysqli→connect_errno ) {
            printf("connect failed: %s<br />", $mysqli→connect_error);
            exit();
         }
         printf('connected successfully.<br />');
   
         if( isset($tutorial_count )) {
            $sql = 'select tutorial_author, tutorial_count
               from  tcount_tbl
               where tutorial_count = ' + $tutorial_count;
         } else {
            $sql = 'select tutorial_author, tutorial_count
               from  tcount_tbl
               where tutorial_count is null';
         }
         $result = $mysqli→query($sql);
           
         if ($result→num_rows > 0) {
            while($row = $result→fetch_assoc()) {
               printf("author: %s, count: %d <br />",
                  $row["tutorial_author"], 
                  $row["tutorial_count"]);               
            }
         } else {
            printf('no record found.<br />');
         }
         $mysqli→close();
      ?>
   </body>
</html>

output

access the mysql_example.php deployed on apache web server and verify the output.

connected successfully.
no record found.