Skip to content

Instantly share code, notes, and snippets.

@imcarvalho
Last active November 10, 2015 08:28
Show Gist options
  • Save imcarvalho/d39a3f6fa26c247e4f43 to your computer and use it in GitHub Desktop.
Save imcarvalho/d39a3f6fa26c247e4f43 to your computer and use it in GitHub Desktop.
Compose an IN for MySQL on PHP.
<?php
/**
* Since PDO doesn't support arrays for the IN natively
* You use the result in the SQL query string as IN($inQuery)
* and $inValues as the $params for the PDO function
* ['A1' => 'first value to find', 'A2' => 'second value to find']
*
* @param array $toFind
* @param string $queryVariableName
* @return array
*/
function getInQuery(array $toFindItems, $queryVariableName){
$inQuery = "";
$inValues = array();
foreach($toFindItems as $key=>$toFindItem){
if($inQuery){
$inQuery .= ",";
}
$queryVariableNameComposed = ":".$queryVariableName.$key;
$inQuery .= $queryVariableNameComposed;
$inValues[$queryVariableNameComposed] = $toFindItem;
}
return [$inQuery, $inValues];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment