-2

I'm currently working with the medoo.php framework, and although I would normally use their ticket area on github, it appears that no one actually uses that... so... At any rate, when I'm running one of my files which uses "require" to call the framework, I get the following error:

Warning: Cannot use a scalar value as an array in /home/..../public_html/projects/friendcodes/medoo.min.php on line 759

However, when I inspect the code (the below is lines 752 to 764), I see that it is in fact supposed to check if $where is not set, and if it isn't, make it an array - however this php error begs to differ.

I'm guessing that $where is being set as a variable somewhere else, that's not an array, but there are over 100 occurrences of the variable in the framework, and 830 lines of code, which you probably don't want to see. (Let me know in a comment and I'll add it - again, this is directly from medoo's most two recent updates/releases.)

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

My main question is - How do I rectify this problem without breaking something in this framework which is extremely complex (for my level, at any rate)

Update: How silly of me! I found the problem. Just as people suggested, I was calling $where wrong. I was calling it with:

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

Instead of

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

(Where the third arg is $where) Thanks for the help guys!

muffinjello
  • 158
  • 1
  • 5
  • 16
  • `if (is_array($where)) $where['LIMIT'] = 1;` – u_mulder Jul 01 '14 at 06:22
  • 1
    If you know the page/function to do to repeat the problem, why not dump the $where variable at the beginning of the get function? Then before it errors out you can see its contents and know what called it and why? – James Hunt Jul 01 '14 at 06:23
  • 1
    How is `->get()` called? More specifically, what's the third argument? You could set up an error handler that catches the warning and then prints the backtrace if you don't know. – Ja͢ck Jul 01 '14 at 06:28
  • Is $where always an array? If it is, then this has a very easy solution. – Evan Darwin Jul 01 '14 at 06:33
  • 1
    If $where is always an array, he wouldn't be getting an error saying it is a scalar.. That's the problem. – James Hunt Jul 01 '14 at 06:35

3 Answers3

1

Right, first things first, we need to find out what is calling get that shouldn't be. WHICH IS THE ENTIRE PROBLEM. The problem isn't the function itself, the problem is something is calling it using an argument for $where which isn't an array. Changing a library to fix one faulty call is ridiculous.

Step 1: Temporarily edit the get function to include a print_r of the $where variable.

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

This will show us before the error prints the value of $where, which will help you find the malformed get call.

If this fails, try using PHP's built-in backtrace to try to find the issue:

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;
}
James Hunt
  • 2,448
  • 11
  • 23
1

The ->get() method is not called properly.

Cannot use a scalar value as an array

That warning is shown if $where is either true, a numeric value or a resource. Valid method calls include:

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

Check the manual and fix your code.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
-2

EDIT 3: try moving $where['LIMIT'] = 1; inside of the isset statement, since you wouldn't want to pass LIMIT 1 to the query constructor if $where is passed by reference.

DISCLAIMER I have no knowledge of the medoo framework.

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;
}
r3wt
  • 4,642
  • 2
  • 33
  • 55
  • This doesn't really answer the question since empty() will return the same as isset(). The scalar warning still exists. – Evan Darwin Jul 01 '14 at 06:28
  • ok, i'll try something else. how bout a ternary statement defaulting to `array()` – r3wt Jul 01 '14 at 06:29
  • That's not the issue with this, PHP is throwing the scalar issue because it's not sure if where is an array or not. – Evan Darwin Jul 01 '14 at 06:30
  • @EvanDarwin I see the issue now. In cases where `$where` is passed by reference and not an array, `$where['LIMIT'] = 1;` should not be applied because a value for where is applied. – r3wt Jul 01 '14 at 06:36
  • The fact that the array is passed by reference makes absolutely no difference. The issue is with the fact that it's unsure of what type it is and refuses to set a value for a key on the object. – Evan Darwin Jul 01 '14 at 06:37