PHP Check for NULL

153,625

Solution 1

Use is_null or === operator.

is_null($result['column'])

$result['column'] === NULL

Solution 2

How about using

if (empty($result['column']))

Solution 3

Make sure that the value of the column is really NULL and not an empty string or 0.

Solution 4

I think you want to use

mysql_fetch_assoc($query)

rather than

mysql_fetch_row($query)

The latter returns an normal array index by integers, whereas the former returns an associative array, index by the field names.

Share:
153,625

Related videos on Youtube

Angel.King.47
Author by

Angel.King.47

Shoot first, so you don't have to ask the questions!

Updated on August 09, 2020

Comments

  • Angel.King.47
    Angel.King.47 over 3 years

    Here is the below Code:

    $query = mysql_query("SELECT * FROM tablex");
    
    if ($result = mysql_fetch_array($query)){
    
        if ($result['column'] == NULL) { print "<input type='checkbox' />"; }
        else { print "<input type='checkbox' checked />"; }
    }
    

    If the values are NOT NULL i still get the uncheked box. Am i doing something wrong from above, shoudnt $result['column'] == NULL work?

    Any Ideas?

    • Scott Evernden
      Scott Evernden over 14 years
      is that really your code? you have a coding error and probably an error_log file
    • Angel.King.47
      Angel.King.47 over 14 years
      Im simply printing the checkbox. And its checked if the value is not null. It dosent seem to do that.. Hense it always goes into the first if statment
    • Scott Evernden
      Scott Evernden over 14 years
      the code posted is missing a right paren and a right brace and i get down-voted for spotting that? eyeroll
    • Angel.King.47
      Angel.King.47 over 14 years
      Sry this might sound mean.. But i didnt ask for syntax problems.. I asked for the Null value.. And if there was syntax error.. the code will output the error. However said that i did see the bracket.. I didnt actually copy and paste my code.. it was more of a type it my self. PS it wasnt me who downvoted you :D
  • Angel.King.47
    Angel.King.47 over 14 years
    sry im using mysql_fetch_array()... put the wrong one in.. Editing now
  • Angel.King.47
    Angel.King.47 over 14 years
    i had results in two places... Its late and my brain is blown up.. Exactly what i did to figure it out :D
  • Anthony Rutledge
    Anthony Rutledge over 7 years
    Using PHP's empty() function leaves too much room for interpretation (php.net/manual/en/function.empty.php) and is more of a language specific solution to a general programming scenario. If he uses ===, he will have a solution that he can use in many languages.
  • Anthony Rutledge
    Anthony Rutledge over 7 years
    That's why fernyb's answer is not a preferable solution.
  • Zoran
    Zoran almost 6 years
    This is not correct because if filed may be null application may have different meaning for the field for example null may represent not checked and 0 false and 1 true
  • Adam
    Adam almost 4 years
    empty also returns true if column is 0
  • Andris
    Andris over 2 years
    $result['column'] === NULL did not work. Worked is_null($result['column']). Php version 7.0.