Ryan Walkowski Ryan Walkowski - 3 months ago 9
MySQL Question

PHP/MySQL Checking for null

$sql = 'SELECT * FROM `phpbb_profile_fields_data`';

$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}

$db->sql_freeresult($result);


I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:

if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')


Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.

Answer

Something like this should work.

if (is_null($row['pf_kp_em_no_bonethr'])) {
  echo "Is NULL";
}