简体   繁体   English

如何检查PHP中的列值是null还是0?

[英]How to check whether column value is null or 0 in PHP?

$query = 'select column from table ...';

...


$row = mysql_fetch_assoc($result);

//now i have the result
//but how to check if it's null or 0?

Pandiya的 意思是is_null

try 尝试

$res=mysql_query(" SELECT NULL AS anullcol, 0 AS anum, '' AS emptystring ");
var_dump(mysql_fetch_assoc($resource));

and read up on the '===' and '!==' operators. 并阅读'==='和'!=='运算符。

C. C。

if($var == '' || $var == NULL){
//Code
}

You can use isset(): 您可以使用isset():

$foo = 0;
echo isset($foo)  ? 'yes' : 'no';
echo "<br>";

$foo = null;
echo isset($foo) ? 'yes' : 'no';

will result in 将导致

yes
no

Maybe you are asking if the Query has returned anything. 也许您在询问查询是否返回了任何内容。 So try using: 因此,请尝试使用:

mysql_num_rows()

from PHP: 从PHP:

Retrieves the number of rows from a result set. 从结果集中检索行数。 This command is only valid for statements like SELECT or SHOW that return an actual result set. 该命令仅对返回实际结果集的SELECT或SHOW语句有效。 To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows(). 要检索受INSERT,UPDATE,REPLACE或DELETE查询影响的行数,请使用mysql_affected_rows()。

<?php
if ( mysql_num_rows ( $query ) == 0){
echo "duh, nothing!";
}
?>

maybe replace the null with an identifier in the select: 也许用select中的标识符替换null:

select coalesce(column, -1) as column 
from table ...

that way you can test for 0 or -1 (NULL). 这样您就可以测试0或-1(NULL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM