Last active
November 10, 2015 08:28
-
-
Save imcarvalho/d39a3f6fa26c247e4f43 to your computer and use it in GitHub Desktop.
Compose an IN for MySQL on PHP.
This file contains 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 | |
/** | |
* 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