Angel.King.47
Angel.King.47

Reputation: 7994

PHP Check for NULL

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?

Upvotes: 52

Views: 169305

Answers (5)

Andrew
Andrew

Reputation: 7768

Sometimes, when I know that I am working with numbers, I use this logic (if result is not greater than zero):

if (!$result['column']>0){

}

Upvotes: -2

fernyb
fernyb

Reputation: 983

How about using

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

Upvotes: 5

Tomas Markauskas
Tomas Markauskas

Reputation: 11596

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

Upvotes: 3

Joel
Joel

Reputation: 11851

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.

Upvotes: 1

The Chairman
The Chairman

Reputation: 7187

Use is_null or === operator.

is_null($result['column'])

$result['column'] === NULL

Upvotes: 114

Related Questions