catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

"Query + IN" without quotes. How? #842

Closed gggglglglg closed 3 years ago

gggglglglg commented 5 years ago

$ids = "1,5,8"; $data = $db->debug()->query( "SELECT * FROM items WHERE 'group' IN (:ids)", [ ":ids" => $ids ] )->fetchAll();

SQL: SELECT * FROM items WHERE 'group' IN ('1,5,8') <-- with quotes '1,5,8'

Need SQL: SELECT * FROM items WHERE 'group' IN (1,5,8)

HOW?

abdul-alim commented 5 years ago

You have defined $ids as string, which is considered a single value. Try passing it as an array.

Alternatively, you can do something like this.

$ids = array(1,5,8);
$database->select("items", "*", [
    "group" => $ids
]);

// Generated SQL SELECT * FROM 'items' WHERE 'group' IN (1, 5, 8)

blessedjasonmwanza commented 4 years ago

@ermaxinc .. Well, as explained above, your $ids is a string. convert it to an array like; $ids = "1,5,8"; $ids = explode(",", $ids)

Opinion :: Also for easy code readability, try not to mix SQL normal statements into Medoo's