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.

This question already has an answer here:

I have an array variable like this

$variable[] = $data['master_software_capability_id'];

How to use this array variable in SQL query

Is this the right way ?

SELECT * FROM software_capability where software_capability_id IN ($variable);
share|improve this question
add comment

marked as duplicate by Shankar Damodaran, Rikesh, Liath, giammin, Sheridan Mar 14 at 8:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 0 down vote accepted

You need to implode the array to comma separated string

$variables_imploded = implode(",",$variable);

"SELECT * 
FROM software_capability 
where software_capability_id IN 
(".$variables_imploded.")";
share|improve this answer
 
Thanks abhik its working. –  Senthilkumar Mar 14 at 7:08
 
You are most welcome !! –  Abhik Chakraborty Mar 14 at 7:08
 
@Senthilkumar please accept this answer as accepted so it will be marked as solved –  CodingAnt Mar 14 at 7:14
add comment

use JOIN or IMPLODE

$variables_joins = join(',',$variable);  

SELECT * FROM software_capability where software_capability_id IN ($variables_joins);
share|improve this answer
add comment

You can implode the array first.. like implode(',',$variable)

"SELECT * FROM software_capability where software_capability_id IN (" . implode(',',$variable)  . ")"
share|improve this answer
 
Thanks Edd A. its working –  Senthilkumar Mar 14 at 7:08
 
Great.. Happy to help! –  Edd A. Mar 14 at 7:19
add comment

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