Skip to content

Instantly share code, notes, and snippets.

@mattattui
Created March 21, 2011 10:32
Show Gist options
  • Save mattattui/879273 to your computer and use it in GitHub Desktop.
Save mattattui/879273 to your computer and use it in GitHub Desktop.
Safe "WHERE … IN" SQL statement in PDO
<?php
$db = new PDO('mysql:host=hostname;dbname=defaultDbName',
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$names = array('Alice', 'Bob', 'Charlie');
$values = array_map(array($db,'quote'),$names);
$query = 'SELECT * FROM my_table WHERE name IN ('.join(',',$values).')';
$result = $db->query($query);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
// ...
}
?>
@mattattui
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment