i have a query like this with an IN clause
sql = "
SOME QUERY WHERE some_item IN ()
";
Do i have to do a for loop to to put all items of the php array into the IN clause or is there a function or method that does this?
regards, alex
|
You could try implode
|
|||
|
http://php.net/manual/en/function.implode.php here is a quick example : |
|||
|
For numeric values in an array:
For string values:
Be careful to escape the values when building the arrays. Hope this helps! |
|||||||
|
If you have the list of things in an array, you can use JOIN to build it for you. It's easiest for numeric values, since you don't have to put quotes around things. Something like this:
If your array contains strings, you should escape that data as you put the data into the array. But once you've done that, you can do something similar to above, adding quote marks.
One potential issue is an empty array, since the above example would give you The above example also doesn't account for NULL values. One way around that is to populate your array with the strings with quotes already around them (so you can have |
|||
|
You can use implode, like:
|
|||
|