Detect null column value in PHP mysqli

mysql, mysqli, php

Solution

Use an `if` condition to check with `===`

if($row['mycolumn'] === null) {
   echo 'Real Null';
} elseif($row['mycolumn'] == '') {
   echo 'Blank';
}

Problem

I have a php project that uses mysqli to query a database. Some of the columns in this database can be null. I have code that looks something like this: ``` $query = "..."; $result = $DB->query($query); $row = $result->fetch_assoc(); $column = $row['mycolumn']; ``` If `mycolumn` is null, the value of `$column` appears to be the string, `"NULL"` (NOT the null value, but actually the string containing the word "NULL"). So what happens if I have columns which actually have the string `"NULL"` in them? How can I differentiate? Thanks! Josh EDIT: Upon closer inspection, it appears that the string is actually a 5-characters string. The first 4 characters are `"NULL"`, but the last character is 0x0d, the carriage return. This makes it a lot easier to detect, although I'm still curious if there's a less hack-y way than just doing string comparison.

Original source