Created
March 21, 2011 10:32
-
-
Save mattattui/879273 to your computer and use it in GitHub Desktop.
Safe "WHERE … IN" SQL statement in PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)) { | |
// ... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://inanimatt.com/php-prepared-statements.php for explanation