Medoo MySQL未读取变量


Medoo MySQL not reading variable

我使用的是Medoo MySQL框架,但在WHERE语句中使用IN时遇到了这个问题:

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

计数结果应该是4,但我得到了1。但是:

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

给我4的正确结果。有什么想法吗?提前感谢!

试试这个。。。

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

$test = '1,2,3,4';是一个字符串。要将其转换为数组,您需要使用:

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

以下解决方案已发布在此处:https://github.com/catfan/Medoo/issues/637

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

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