【问题标题】:PHP Error: Cannot use a scalar value as an array... HoweverPHP 错误:不能将标量值用作数组...但是
【发布时间】:2014-07-01 06:20:32
【问题描述】:

我目前正在使用 medoo.php 框架,虽然我通常会使用他们在 github 上的票务区域,但似乎没有人真正使用它......所以...... 无论如何,当我运行一个使用“require”调用框架的文件时,我收到以下错误:

警告:在第 759 行的 /home/..../public_html/projects/friendcodes/medoo.min.php 中,不能将标量值用作数组

但是,当我检查代码时(下面是第 752 到 764 行),我发现它实际上应该检查 $where 是否未设置,如果未设置,请将其设为数组 - 但是这个 php 错误需要不同。

我猜$where 被设置为其他地方的变量,这不是一个数组,但在框架中有超过 100 次出现的变量和 830 行代码,你可能没有想看。 (在评论中让我知道,我会添加它 - 再次,这直接来自 medoo 最近的两个更新/发布。)

public function get($table, $columns, $where = null)
{
    if (!isset($where))
    {
        $where = array();
    }

    $where['LIMIT'] = 1;

    $data = $this->select($table, $columns, $where);

    return isset($data[0]) ? $data[0] : false;
}

我的主要问题是 - 我如何在不破坏这个框架中极其复杂的东西的情况下纠正这个问题(无论如何,就我的水平而言)

更新:我真傻!我发现了问题。正如人们所建议的那样,我调用 $where 错误。 我打电话给它:

$accountinfo = $database->get('xf_user_field_value', ['field_value'], 1);

代替

$accountinfo = $database->get('xf_user_field_value', ['field_value'], ["user_id"=>1]);

(第三个参数是 $where)感谢大家的帮助!

【问题讨论】:

  • if (is_array($where)) $where['LIMIT'] = 1;
  • 如果你知道要重复问题的页面/函数,为什么不在 get 函数的开头转储 $where 变量呢?然后在它出错之前,您可以看到它的内容并知道它的名称和原因?
  • ->get() 怎么称呼?更具体地说,第三个论点是什么?您可以设置一个错误处理程序来捕获警告,然后在您不知道的情况下打印回溯。
  • $where 总是一个数组吗?如果是,那么这是一个非常简单的解决方案。
  • 如果 $where 始终是一个数组,他就不会收到错误,说它是一个标量。这就是问题所在。

标签: php arrays medoo


【解决方案1】:

对,首先,我们需要找出不应该调用get 的内容。这是整个问题。问题不在于函数本身,问题在于使用$where 的参数调用它,而不是数组。更改一个库来修复一个错误的调用是荒谬的。

第 1 步:临时编辑 get 函数以包含 $where 变量的 print_r

public function get($table, $columns, $where = null)
{
    if(isset($where)) print_r($where);
    if (!isset($where))
    {
        $where = array();
    }

    $where['LIMIT'] = 1;

    $data = $this->select($table, $columns, $where);

    return isset($data[0]) ? $data[0] : false;
}

这将在错误打印$where 的值之前向我们显示,这将帮助您找到格式错误的get 调用。

如果失败,请尝试使用 PHP 的内置回溯来尝试查找问题:

public function get($table, $columns, $where = null)
{
    if(isset($where)) print_r(debug_backtrace());
    if (!isset($where))
    {
        $where = array();
    }

    $where['LIMIT'] = 1;

    $data = $this->select($table, $columns, $where);

    return isset($data[0]) ? $data[0] : false;
}

【讨论】:

  • 感谢回溯提示!我现在正在努力。
【解决方案2】:

->get() 方法调用不正确。

不能将标量值用作数组

如果$wheretrue、数值或资源,则会显示该警告。有效的方法调用包括:

->get('table', '*')
->get('table', '*', array('WHERE' => 'foo = "bar"'))

检查manual 并修复您的代码。

【讨论】:

    【解决方案3】:

    编辑 3:尝试将 $where['LIMIT'] = 1; 移动到 isset 语句中,因为如果 $where 通过引用传递,您不希望将 LIMIT 1 传递给查询构造函数。 p>

    免责声明我不了解 medoo 框架。

    public function get($table, $columns, $where = null)
    {
        if (is_null($where))
        {
            $where = array('LIMIT'=>1);
        }
    
    
    
        $data = $this->select($table, $columns, $where);
    
        return isset($data[0]) ? $data[0] : false;
    }
    

    【讨论】:

    • 这并不能真正回答问题,因为 empty() 将返回与 isset() 相同的结果。标量警告仍然存在。
    • 好的,我会试试别的。如何设置默认为 array() 的三元语句
    • 这不是问题所在,PHP 抛出了标量问题,因为它不确定数组在哪里。
    • @EvanDarwin 我现在看到了这个问题。如果 $where 通过引用而不是数组传递,则不应应用 $where['LIMIT'] = 1;,因为应用了 where 的值。
    • 数组通过引用传递的事实完全没有区别。问题在于它不确定它是什么类型并且拒绝为对象上的键设置值。
    猜你喜欢
    • 2014-07-11
    • 2019-12-17
    • 2011-12-02
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    相关资源
    最近更新 更多