Chris Chris - 4 months ago 3x
PHP Question

Create SQL from array

I'm trying to create a SQL Query that gets data from my DB depending on what the array includes.


My array includes

1, 2, 3
then the query should be
SELECT * FROM v WHERE category='1' OR category='2' OR category='3'

Does anyone know a way to achieve this?

Any tips are welcome.


Using MySQL as DB.


You can use implode function and IN clause as

$sql="SELECT * FROM v WHERE category IN ('".implode("','", $your_array)."')";