0

I'm using the Medoo MySQL framework but ran into this issue when using IN within a WHERE statement:

$test = '1,2,3,4';

$count = $database->count("products", [
    "AND" => [
        "category_id" => $category['id'],
        "id" => [$test]
    ]
]);

The count result should be 4, but I'm getting 1. However:

$count = $database->count("products", [
    "AND" => [
        "category_id" => $category['id'],
        "id" => [1,2,3,4]
    ]
]);

Gives me the correct result of 4. Any ideas? Thanks in advance!

  • my guess is `$test = '1,2,3,4';` in `"id" => [$test]` is evaluating to array with 1 string, ie. `['1,2,3,4']` or `id[0] = `1,2,3,4`, instead of an array with 4 values. – Sean Mar 29 '16 at 05:30

3 Answers3

1

Try this...

$test = array(1,2,3,4);

$count = $database->count("products", [
    "AND" => [
        "category_id" => $category['id'],
        "id" => $test // variable without []
    ]
]);
jkrav
  • 11
  • 2
0

$test = '1,2,3,4'; is a string. To convert it as array, you need to use:

$test = explode(',', '1,2,3,4');

$count = $database->count("products", [
    "AND" => [
        "category_id" => $category['id'],
        "id" => $test
    ]
]);
Angolao
  • 986
  • 1
  • 15
  • 27
0

The following solution has been posted here: https://github.com/catfan/Medoo/issues/637

$values = '2,123,234,54';

$database->select("account", "user_name", [
    "OR" => [
        "user_id" => explode(',',$values)
    ]
]);