1

how to check mysql NULL columns variable with php?

my rows table (example):

1 ---- james ------ italy

2 ---- joyee ------ NULL

my fetch array from table:

$rows[0] = array('name' => 'james', 'country' => 'italy');
$rows[1] = array('name' => 'joyee', 'country' => NULL);

but my problem:

how to check NULL array element:

foreach($rows as $row) {

     if ($row['country'] == NULL) echo "country is null"; // not working
     if ($row['country'] == '') echo "country is null"; // not working
     if (is_null($row['country'])) echo "country is null"; // not working
     if (empty($row['country'])) echo "country is null"; // not working
     // what is  solution? how to check null?

}

update (var_dump):

'country' => null
7
  • Try null in lower case. Commented May 29, 2017 at 17:43
  • can you update your post with var_dump($rows[1]) ? Commented May 29, 2017 at 17:44
  • 1
    Possible duplicate of PHP Check for NULL Commented May 29, 2017 at 17:45
  • @NigelRen not working Commented May 29, 2017 at 17:45
  • @SahilGulati updated! Commented May 29, 2017 at 17:51

1 Answer 1

6

Use is_null or === operator.

is_null($row['country'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.