PHP 检查 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1576243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:08:51  来源:igfitidea点击:

PHP Check for NULL

phpmysqlnull

提问by Angel.King.47

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 NULLi still get the uncheked box. Am i doing something wrong from above, shoudnt $result['column'] == NULLwork?

如果值是NOT NULL我仍然得到未选中的框。我从上面做错了什么,应该$result['column'] == NULL工作吗?

Any Ideas?

有任何想法吗?

回答by The Chairman

Use is_nullor ===operator.

使用is_null===运算符。

is_null($result['column'])

$result['column'] === NULL

回答by fernyb

How about using

怎么用

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

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

回答by Tomas Markauskas

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

确保该列的值确实为 NULL 而不是空字符串或 0。

回答by Joel

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.

后者按整数返回一个普通数组索引,而前者返回一个关联数组,按字段名称索引。

回答by Andrew

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

有时,当我知道我在处理数字时,我会使用以下逻辑 ( if result is not greater than zero):

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

}