2017-05-29 29 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