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 re开发者_运维问答sult
//but how to check if it's null or 0?
What Pandiya was meant to say was 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.
if($var == '' || $var == NULL){
//Code
}
You can use 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:
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. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
<?php
if ( mysql_num_rows ( $query ) == 0){
echo "duh, nothing!";
}
?>
maybe replace the null with an identifier in the select:
select coalesce(column, -1) as column
from table ...
that way you can test for 0 or -1 (NULL).
精彩评论