Storing a Yes / No Button in a MySQL Database

I saw that the value of the Yes / No radio button of the form is saved / saved in several ways. I wonder which way is better and why? This is for a PHP / MySQL application with a typical yes / no question as part of the form.

1.) Save it as 1, 0 or null. 1 - Yes, 0 - No, but null - did not answer.

2.) Save it as Yes, No, No. Assume that a language conversion can be performed.

3.) Use 1, 2, and null to better distinguish values.

Thanks Jeff

Edit: I should also mention that most of the problems were due to jQuery / JavaScript, as well as $ () comparisons and bindings.

+5
source share
6 answers

Since MySQL is of type BOOLEAN, but it's just an alias of TINYINT. I recommend against it, because the equal sign in PHP == will not distinguish 0 from a lack of value. You always need to use a triple equal ===, and it would be easy to make a mistake.

As for your options:

  • This seems like a natural choice with PHP, but then you have to be careful to distinguish 0 from lack of value, so I would not recommend it.

  • I would not recommend this one.

  • Perhaps, but assignment 1 and 2 are somewhat arbitrary and it can be difficult to remember and read in code.

Usually I use "Y", "N" and "NULL" in the CHAR (1) field, it reads the code well and does not cause problems.

+4
source

TINYINT(1). NULL, " ". BOOLEAN, . MySQL.

: MySQL

+6

0/1/null No/Yes/Blank. 0 false 1 true.

0

, , 1 "", 0 "" "NO value" "null", , .

, , , , 0/1, -

if(table.field == 1)
    echo yes;
else
    echo no;

.

0

( , ,...) mysql, , 3 (1, 0 )   1 0 (0000000 0000001), , .

, u .

0

, ENUM . :

ENUM('no','yes')

NULL . "" - 0 ( "" ) 1, "". - "" "" 1 0. 0 1. , TINYINT.

0

All Articles