A Quick Tip

I haven’t written anything here for ages due to illness, work and life getting in the way. I’ve got a longer post brewing that I’ll hopefully add in the next couple of days but for now here’s a quick tip that I hope someone will find useful.

I recently had a situation where I wanted to use an array of values as bound parameters in a SQL IN clause. Easy enough to do except the array was of variable length and I didn’t know how long it would be each time the script was ran. Here’s the solution I came up with.


try {

$array = array('some value', 'another', 'another');//Variable length array, unknown length before runtime


$sql = "SELECT SomeColumn FROM table WHERE SomeOtherColumn IN (" . implode(',', array_fill(0, count($array), '?')) . ') ORDER BY SomeColumn';

$st = $db->prepare($sql);



catch (PDOExeception $e) {}


Quick and easy!

Leave a Reply