Bind arrays for IN(...) queries and scalar values in PDO

Uses PDO prepared statements to bind both scalar and array for IN(...) queries. Read more.

php
fork add to bundle source
function mysql_bind_advanced($sql, $bind = []) { $handle = new PDO('mysql:host=127.0.0.1;dbname=db', 'user', 'pwd'); foreach ( $bind as $key => $value ) { if ( is_array($value) ) { unset($bind[$key]); $in = []; foreach ( $value as $index => $sub_value ) { $in[] = "{$key}{$index}"; $bind[$key . $index] = $sub_value; } $sql = str_replace($key, implode(', ', $in), $sql); } } $st = $handle->prepare($sql); $st->execute($bind); return $st; }

usage

load_function('mysql_bind_advanced'); $statement = mysql_bind_advanced( 'SELECT * FROM users WHERE id IN (:ids)', [':ids' => [1, 2, 3]] ); print_r($statement->fetchAll()); // ...
← how to use this in app?
create log ...inout