PHP 错误:不能将标量值用作数组.然而


PHP Error: Cannot use a scalar value as an array... However

我目前正在使用 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(谢谢你们的帮助!

对,首先,我们需要找出不应该调用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;
}

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

不能将标量值用作数组

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

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

检查手册并修复您的代码。

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

免责声明 我对 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;
}