Chris Chris - 1 year ago 98
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.

Answer Source

You can use implode function and IN clause as

$sql="SELECT * FROM v WHERE category IN ('".implode("','", $your_array)."')";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download