php - CakePHP 检查数据库中的特定值是否为 true

标签 php mysql cakephp

下午好,所以我想检查我的数据库中的特定值在 CakePHP 上是否正确。
我的 SQL 表名为 Oral,如下所示

    ---------------------
    |PK|OralPresentation|
    ---------------------
    |1 |1               |
    ---------------------

所以我想要在我的 Controller 中做的是检查该 Value is OralPresentation 是否为 1。
我在下面的代码中这样做

            $OralStatus = $this->Submission->query("SELECT * FROM Oral;");
            //If Oral Presentations are activated
            if($OralStatus['0']['PK']['OralPresentation']==1)
            {
                $this->set('Istrue','true'); //Passing this information to the view.
            }
            else
            {
                $this->set('Istrue','false');
            }
        }

现在,当我在我看来 echo isTrue 时,尽管数据库表为 1,它总是返回 false。我尝试过的另一个解决方案是在 IF 语句中

if($OralStatus['0']['PK']['OralPresentation'])

最佳答案

如果您知道要检查的 PK,则一种方便的方法是使用 field

$this->Oral->id = 1;
$Istrue = $this->Oral->field('OralPresentation'); // value for pk 1
$this->set(compact('Istrue'));

cake docs on field

php compact

关于php - CakePHP 检查数据库中的特定值是否为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39731178/

相关文章:

php - 从函数返回并回显我的数组

mysql - 从多个表中获取值(value)

php - api cakephp 休息认证

php - 在php/mysql中搜索数组查询结果

php - 如何在一行中显示汇总表和其他表数据?

mysql - SQL 查询返回适用于类别中包含的产品的过滤器,并且一旦 1+ 个过滤器处于事件状态,则适用于剩余产品的过滤器

php - 在 CakePHP 中编辑创建新条目而不是更新旧条目

Cakephp表单单选按钮标签类

PHP - 使用 MD5 和 MySQL 验证密码

mysql - 从 MySQL 中的 CSV 文件导入数据