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.
<?php
try {
$array = array('some value', 'another', 'another');//Variable length array, unknown length before runtime
$db = new PDO(CONNSTR, USERNAME, PASS);
$sql = "SELECT SomeColumn FROM table WHERE SomeOtherColumn IN (" . implode(',', array_fill(0, count($array), '?')) . ') ORDER BY SomeColumn';
$st = $db->prepare($sql);
$st->execute($array);
}
catch (PDOExeception $e) {}
?>
Quick and easy!
