【问题标题】:Condition to check a BIT field检查 BIT 字段的条件
【发布时间】:2013-04-21 06:34:49
【问题描述】:

...我正在检查这个字段:

if((bool)$website['IsDeleted']) { }

但它总是返回一个空字符串,无论 MySQL 字段中的值是它的0 还是1

["IsDeleted"]=> string(1) "" }

请告诉我我在这里做什么?是否应该修改if 条件?

【问题讨论】:

  • 不是if($website['IsDeleted']) { }吗?
  • @Amir - 他将变量 $website['IsDeleted'] 转换为布尔值 - 这没什么问题。
  • @Nimbuz 那么你的意思是它总是做else 声明无论是0还是1?

标签: php mysql boolean conditional-statements


【解决方案1】:

我看不出你的 if 语句有什么问题。

<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo 'found you';}
?>

会输出 找到你了

<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo $website['IsDeleted'];}
?>

将输出 1

如果您的值为 NULL,那么它将不起作用。然后您还必须检查 NULL 值。

<?php
$website['IsDeleted'] = null;
if((bool)$website['IsDeleted'] || $website['IsDeleted'] === null) { echo 'found you';}
?>

输出会找到你。

【讨论】:

  • 我无法手动设置值。必须从数据库中获取并检查。
  • 似乎从数据库中获取的值是实际的问题(而不是 if 语句)。你是如何进行抓取的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多