PHP错误:不能将标量值用作数组...但是

时间:2014-07-01 06:20:32

标签: php arrays medoo

我目前正在使用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;
}

我的主要问题是 - 如何在不违反此框架的情况下纠正此问题,这非常复杂(对于我的级别,无论如何)

更新:我真傻!我发现了这个问题。就像人们建议的那样,我在说错了。 我用它来称呼它:

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

而不是

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

(第三个arg是$ where)感谢帮助人员!

3 个答案:

答案 0 :(得分:1)

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

第1步:暂时修改get功能,以包含print_r变量的$where

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;
}

答案 1 :(得分:1)

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

  

不能将标量值用作数组

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

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

检查manual并修复您的代码。

答案 2 :(得分:-2)

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

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

$where
相关问题