2017-05-29 27 views
0

如何用php查询mysql NULL列变量?php mysql如何用php检查NULL列

我行表(例如):

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

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

我从表中获取数组:

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

但我的问题:

如何检查NULL数组元件:

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? 

} 

更新(的var_dump):

'country' => null 
+0

以小写字母试试null。 –

+0

你可以用var_dump($ rows [1])更新你的文章吗? –

+1

可能重复的[PHP检查NULL](https://stackoverflow.com/questions/1576243/php-check-for-null) –

回答

4

使用is_null或===运算符。

is_null($row['country']) 
+0

是的。谢谢 – grizzly