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.
Related videos on Youtube
Author by
Angel.King.47
Shoot first, so you don't have to ask the questions!
Updated on August 09, 2020Comments
-
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 over 14 yearsis that really your code? you have a coding error and probably an error_log file
-
Angel.King.47 over 14 yearsIm 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 over 14 yearsthe code posted is missing a right paren and a right brace and i get down-voted for spotting that? eyeroll
-
Angel.King.47 over 14 yearsSry 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 over 14 yearssry im using mysql_fetch_array()... put the wrong one in.. Editing now
-
Angel.King.47 over 14 yearsi had results in two places... Its late and my brain is blown up.. Exactly what i did to figure it out :D
-
Anthony Rutledge over 7 yearsUsing 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 over 7 yearsThat's why fernyb's answer is not a preferable solution.
-
Zoran almost 6 yearsThis 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 almost 4 yearsempty also returns true if column is 0
-
Andris over 2 years
$result['column'] === NULL
did not work. Workedis_null($result['column'])
. Php version 7.0.