Khurram Bashir
Khurram Bashir

Reputation: 41

Data field null value return 0

I am storing a radio button value which can be true, false or NULL in a database. In case of null or false the answer is 0 that is false. Is there any solution for checking the null value? I have tried using isset and Empty but none of these help. Database is MSSQL and datatype is bit to store the value of radio button

<input name="radioVal" type="radio" id="radioVal" value="false"
<?PHP
           if(row['radioVal'] == false ) 
              echo "checked='checked' "; 
}
?> />

<input name="radioVal" type="radio" id="radioVal" value="true"
<?PHP
           if(row['radioVal'] == true ) 
              echo "checked='checked' "; 
}
?> />

Upvotes: 4

Views: 163

Answers (3)

Stefan Deutschmann
Stefan Deutschmann

Reputation: 238

Maybe you should use another data type in your SQL Database.

I suggest Enum or something like that:

ENUM('false', 'true', 'NULL')

In your PHP file you

<?PHP
       if(row['radioVal'] == false ) 
          echo "checked='checked' "; 
}
?>

Upvotes: 0

Asaph
Asaph

Reputation: 162831

The bit column is probably going to be represented as a 1 or a 0. Use the triple equals to check for value and type. Check if row['radioVal'] === 1 for true, row['radioVal'] === 0 for false, and row['radioVal'] === null for null.

Upvotes: 1

xkeshav
xkeshav

Reputation: 54050

TRY

 if(false === row['radioVal'] ) //or
 if(NULL === row['radioVal'] )

 

and to check for NULL you can check with is_null

 is_null(row['radioVal'])

Upvotes: 1

Related Questions