user2810718 user2810718 - 1 year ago 70
MySQL Question

Create WHERE IN() clause from php array with quotes

I have a PHP array as such :

(0 => assssssss, 1 => camo, 2 => castor)

I am trying to build a query string like this:

WHERE IN ('assssss','camo','castor')

I have tried using implode which returns a string but without the single quotes like so:


looping throught the array adding quotes ,adding commas detecting last value seems a bit tedious.

Is there any way to obtain the string required?

Answer Source

Try the below method

$query = "SELECT column1 FROM table WHERE column1 IN ('".implode("','",$array)."')";

Your query will get the required format for execution with WHERE clause.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download