Chris Chris - 5 months ago 5
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.

Example:

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.

UPDATE:

Using MySQL as DB.

Answer

You can use implode function and IN clause as

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