Reputation: 2000
So I have a dynamic built query using the 'IN' clause, but really need to select for two parameters, and cannot seem to figure out how to do this.
$selectSql= "SELECT groupid FROM groups WHERE name IN(";
foreach ($groupsArray as $group => $row){
$selectSql .= "'".$row['name']."'".",";
}
$selectSql = rtrim($selectSql, ',');
$selectSql .= ")";
Really, the query needs to look something like:
$selectSql= "SELECT groupid FROM groups WHERE name and type IN(;
How would I go about doing this?
Sincere thanks for any help! it is greatly appreciated.
Upvotes: 2
Views: 451
Reputation: 36
I also support implode()
but done in a slightly different way:
$name = array();
$type = array();
foreach ($groupsArray as $group => $row ) {
$name[] = $row['name'];
$type[] = $row['type'];
}
$selectSql= "SELECT groupid FROM groups WHERE name IN ('". implode("' ,'", $name) ."') and type IN ('". implode("', '", $type). "')";
In this way you do not have to change the array to insert the '
to each item because it is done by implode.
I did not understand well what he's like your array, but you may even direct access $groupsArray
items without creating $name
and $type
.
Upvotes: 2
Reputation: 13728
Try implode()
foreach ($groupsArray as $group => $row ){
$name[]= "'".$row['name']."'";
$type[] = "'".$row['type']."'";
}
$selectSql= "SELECT groupid FROM groups WHERE name IN(".implode(',',$name).") and type IN(".implode(',',$type).")";
Upvotes: 3