user2810718 user2810718 - 2 months ago 8
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:


asssss,camo,cator


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

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.