Reputation: 4807
$skuArray = array(00240=>123,00241=>456);
$getSkus = mysql_query("SELECT sku FROM data WHERE sku IN($skuArray)");
My above code doesn't work, how can I make it SELECT all sku's FROM data WHERE sku = any of the key names in $skuArray? (00240 and 00241 in this case)
Hope this makes sense, Thank You.
Upvotes: 0
Views: 2645
Reputation: 1827
foreach($skuArray as $value=>$key){
if($where=="")
$where= $value" = '".$key."'";
else
$where.= $value" = '".$key."'";
}
$getSkus = mysql_query("SELECT sku FROM data " .($where)?" WHERE $where )":"");
Upvotes: 0
Reputation: 2293
Try this:
<?php
$skuArray = array('00240'=>123, '00241'=>456);
$inSkus = array();
foreach (array_keys($skuArray) as $key)
{
$inSkus[] = '"' . $key . '"';
}
$sql = 'SELECT sku FROM data WHERE sku IN (' . implode(', ', $inSkus) . ')';
echo $sql;
You need to have the keys as strings and you then need to wrap them in parentheses for the SQL query.
Upvotes: 2
Reputation: 3297
$skuArray = array('00240'=>123,'00241'=>456);
$keys = array_keys($skuArray);
$getSkus = mysql_query("SELECT sku FROM data WHERE sku IN('" . join("','", $keys) . "')");
Upvotes: 2