Currently I'm using Tinyint(1)
to indicate Boolean
values in my MySQL databases, which I really don't like that. So, how could I store and retrieve Boolean
values in my MySQL
databases via PHP
?
目前我正在使用Tinyint(1)来指示MySQL数据库中的布尔值,我真的不喜欢这样。那么,我如何通过PHP在我的MySQL数据库中存储和检索布尔值?
How to use it in WHERE
clause and how to assign the value in INSERT, UPDATE
queries properly?
如何在WHERE子句中使用它以及如何在INSERT,UPDATE查询中正确分配值?
When I have it back on PHP, it's TRUE
, true
, or simply 1
, if I'm gonna check that with ===
?
当我把它恢复到PHP时,它是真的,真的,或者只是1,如果我要用===检查它?
Also did you ever had any problem when you migrating from Tinyint(1)
to BOOLEAN
?
当你从Tinyint(1)迁移到BOOLEAN时,你也遇到过任何问题吗?
Thanks in advance. :)
提前致谢。 :)
Update:
更新:
I know that Tinyint(1)
is the same as Boolean
, however I want to work on Boolean
data type instead of Tinyint(1)
. That's why I'm asking the question.
我知道Tinyint(1)与Boolean相同,但我想处理布尔数据类型而不是Tinyint(1)。这就是我问这个问题的原因。
2 个解决方案
#1
16
MySQL doesn't have a boolean data type. Tinyint(1) is pretty close enough. Working with this in PHP is simple.
MySQL没有布尔数据类型。 Tinyint(1)非常接近。在PHP中使用它很简单。
If (1) echo 'true'; // is the same as if (true)
// Just as
if (0) echo 'false'; // is the same as if (false)
And if you really really want a boolean value, you can do
如果你真的想要一个布尔值,你可以做到
// $mysql_data is tinyint value from db
$boolean = $mysql_data ? true : false;
// Now you have your boolean as $boolean
#2
4
With booleans, don't use === FALSE
- the value is already boolean (unless the function requires you to use ===
, like strpos()
). The value is booleanish - it's technically integer, but PHP is dynamic language, so it isn't problem.
使用布尔值时,不要使用=== FALSE - 该值已经是布尔值(除非该函数要求您使用===,如strpos())。值是booleanish - 它在技术上是整数,但PHP是动态语言,所以它不是问题。
Consider preg_match()
function - it returns number of matches (integer).
考虑preg_match()函数 - 它返回匹配数(整数)。
Would you prefer to write that?
你愿意写那个吗?
if (preg_match('/\bregexp?\b/', $variable) === 1)
Or that?
或者那个?
if (preg_match('/\bregexp?\b/', $variable))
Obviously, the way without explicit === 1
is better. You ask if it matches, not if it has 0 matches. Also, if you think that === 1
is safer, why not do === 1 === TRUE
?
显然,没有显式=== 1的方式更好。你问它是否匹配,而不是它是否匹配0。另外,如果您认为=== 1更安全,为什么不这样做=== 1 === TRUE?
Of course, it's possible to convert values to booleans using (bool)
or !!
.
当然,可以使用(bool)或!!将值转换为布尔值。
Also, in certain languages such as C or Perl, there is no difference between booleans and numbers. It just works.
此外,在某些语言(如C或Perl)中,布尔值和数字之间没有区别。它只是有效。
#1
16
MySQL doesn't have a boolean data type. Tinyint(1) is pretty close enough. Working with this in PHP is simple.
MySQL没有布尔数据类型。 Tinyint(1)非常接近。在PHP中使用它很简单。
If (1) echo 'true'; // is the same as if (true)
// Just as
if (0) echo 'false'; // is the same as if (false)
And if you really really want a boolean value, you can do
如果你真的想要一个布尔值,你可以做到
// $mysql_data is tinyint value from db
$boolean = $mysql_data ? true : false;
// Now you have your boolean as $boolean
#2
4
With booleans, don't use === FALSE
- the value is already boolean (unless the function requires you to use ===
, like strpos()
). The value is booleanish - it's technically integer, but PHP is dynamic language, so it isn't problem.
使用布尔值时,不要使用=== FALSE - 该值已经是布尔值(除非该函数要求您使用===,如strpos())。值是booleanish - 它在技术上是整数,但PHP是动态语言,所以它不是问题。
Consider preg_match()
function - it returns number of matches (integer).
考虑preg_match()函数 - 它返回匹配数(整数)。
Would you prefer to write that?
你愿意写那个吗?
if (preg_match('/\bregexp?\b/', $variable) === 1)
Or that?
或者那个?
if (preg_match('/\bregexp?\b/', $variable))
Obviously, the way without explicit === 1
is better. You ask if it matches, not if it has 0 matches. Also, if you think that === 1
is safer, why not do === 1 === TRUE
?
显然,没有显式=== 1的方式更好。你问它是否匹配,而不是它是否匹配0。另外,如果您认为=== 1更安全,为什么不这样做=== 1 === TRUE?
Of course, it's possible to convert values to booleans using (bool)
or !!
.
当然,可以使用(bool)或!!将值转换为布尔值。
Also, in certain languages such as C or Perl, there is no difference between booleans and numbers. It just works.
此外,在某些语言(如C或Perl)中,布尔值和数字之间没有区别。它只是有效。