It seems I should use tinyint(); but I don't know how to implement it?
The question is what is your recommendation if I need to have a boolean field in MySQL DB and modify it´s value with PHP
You can use tinyint(1) or bool or boolean. All are synonym. If you use bool or boolean datatype, then it nternally change into tinyint(1). In PHP, the value 0 represents false and 1 represents true.
You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.
To deal with Boolean in MySQL, you can use BOOL or BOOLEAN or TINYINT(1). If you use BOOL or BOOLEAN, then MySQL internally converts it into TINYINT(1). In BOOL or BOOLEAN data type, if you use true literal then MySQL represents it as 1 and false literal as 0 like in PHP/ C/ C++ language.
To create a column with 'false' as the default value, we can use the concept of “default” at the time of creation of the table. Note − 0 represents false and 1 represents true. Creating a table using “default” false.
Yep, TINYINT(1)
is the way to go... you can also use BOOL
or BOOLEAN
which are synonyms (so it does not make a difference).
0
evaluates to false
in PHP and 1
to true
(actually, any other number than 0
evaluates to true
, but 1
is normally used).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With