0
$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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
$skuArray = array('00240'=>123,'00241'=>456);
$keys = array_keys($skuArray);
$getSkus = mysql_query("SELECT sku FROM data WHERE sku IN('" . join("','", $keys) . "')");

1 Comment

What's not working? Are the array keys wrapped in quotes, otherwise PHP reads them as octals. Is $skuArray NULL? In that case you'll get an error on line 2. You should probably check that $skuArray is not empty as well.
0
 foreach($skuArray  as $value=>$key){
    if($where=="")
       $where= $value" = '".$key."'";
    else
       $where.= $value" = '".$key."'";
 }
 $getSkus = mysql_query("SELECT sku FROM data " .($where)?" WHERE   $where )":"");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.