user198729
user198729

Reputation: 63646

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?

Upvotes: 1

Views: 869

Answers (6)

MoeAmine
MoeAmine

Reputation: 6076

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!";
}
?>

Upvotes: 0

user254875486
user254875486

Reputation: 11240

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

Upvotes: 0

symcbean
symcbean

Reputation: 48357

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.

Upvotes: 1

Patrick Beardmore
Patrick Beardmore

Reputation: 1032

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

Upvotes: 0

Ben Everard
Ben Everard

Reputation: 13804

What Pandiya was meant to say was is_null.

Upvotes: 3

davek
davek

Reputation: 22915

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).

Upvotes: -1

Related Questions