Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array and I wrote this code which does not work. Could someone help me with this query? Thanks a lot!

 $sql = "SELECT * FROM `product` WHERE `product`.`Productcode` IN (".$array.") ";
         $result=mysql_query($sql);
         if(!$result)die('not exist');
share|improve this question

closed as unclear what you're asking by Lightness Races in Orbit, fancyPants, JoseK, Ismael Abreu, Pete Jun 27 '13 at 11:44

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. –  STT LCU Jun 27 '13 at 9:27
1  
Do a var_dump on the $array. –  John V. Jun 27 '13 at 9:34
1  
-1 Because, according to comments, this table doesn't even exist in your database. You'd failed to do proper error checking and therefore only just found out. Also "does not work" is horribly useless. –  Lightness Races in Orbit Jun 27 '13 at 9:46
    
@darya wow this goes straight in my top 10 of worst askers in StackOverflow. Congrats! –  STT LCU Jun 27 '13 at 9:48
    
thanks i found my error . thanks alot:) –  Darya Jun 27 '13 at 9:56

4 Answers 4

the array should be a string

$string = implode("','", $array);
$sql = "SELECT * FROM `product` WHERE `product`.`Productcode` IN ('".$string."') ";

quotes not needed if the productcode is an integer

share|improve this answer
    
Note the quotes aren't needed if the array contains integers. –  GolezTrol Jun 27 '13 at 9:26
    
when i wrote this , i have error !!! –  Darya Jun 27 '13 at 9:30
    
and if(!$result)die('not exist'); display in my webpage ?? –  Darya Jun 27 '13 at 9:31
    
@Darya instead of die('not exists') write die(mysql_error()) and tell us what do you see –  STT LCU Jun 27 '13 at 9:34
2  
@Darya Now you have the information you need to debug your code. Good luck. –  Lightness Races in Orbit Jun 27 '13 at 9:45

Try this if your array is not multidimentional

$sql = "SELECT * FROM `product` WHERE `product`.`Productcode` IN ('".implode("',", $array)."') ";
share|improve this answer
    
Your implode argument is wrong. –  Lightness Races in Orbit Jun 27 '13 at 9:45

You need to implode() the array to a string (presuming your $array is actually an array):

$sql = "SELECT * FROM `product` WHERE `product`.`Productcode` IN ('" . implode("', '", $array) . "')";
share|improve this answer
    
when i wrote this , i have error !!! and if(!$result)die('not exist'); display not exist in my webpage ?? –  Darya Jun 27 '13 at 9:37

You need to implode the array to a comma separated list before it can be used IN()

IN(".implode(',',$array).")
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.