处理 PHP 和 MySQL 中的“布尔”值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12660756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:59:31  来源:igfitidea点击:

To deal with 'Boolean' values in PHP & MySQL

phpmysqlboolean

提问by Mahdi

Currently I'm using Tinyint(1)to indicate Booleanvalues in my MySQL databases, which I really don't like that. So, how could I store and retrieve Booleanvalues in my MySQLdatabases via PHP?

目前我正在使用Tinyint(1)来指示Boolean我的 MySQL 数据库中的值,我真的不喜欢那样。那么,我如何通过 存储和检索数据库中的Boolean值?MySQLPHP

How to use it in WHEREclause and how to assign the value in INSERT, UPDATEqueries 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 上时,它是TRUE, true,或者只是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 Booleandata type instead of Tinyint(1). That's why I'm asking the question.

我知道这Tinyint(1)与 相同Boolean,但是我想处理Boolean数据类型而不是Tinyint(1). 这就是我问这个问题的原因。

回答by kehers

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

回答by Konrad Borowski

With booleans, don't use === FALSE- the value is already a boolean (unless the function requires you to use ===, like strpos()). The value is booleanish - it's technically an integer, but PHP is a dynamic language, so it its not a problem.

对于布尔值,不要使用=== FALSE- 该值已经是布尔值(除非函数要求您使用===,例如strpos())。该值是布尔值 - 从技术上讲它是一个整数,但 PHP 是一种动态语言,所以它不是问题。

Consider preg_match()function - it returns the 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 === 1is better. You ask if it matches, not if it has 0 matches. Also, if you think that === 1is 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)or将值转换为布尔值!!

Also, in certain languages such as C or Perl, there is no difference between booleans and numbers. It just works.

此外,在某些语言(例如 C 或 Perl)中,布尔值和数字之间没有区别。它只是有效。