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:

Hello All

I have a simple php array, For Example:

$numbers=array("12345","65432","98765");

And SQL Table named: "phones" with the column "TNumbers" and the rows: 654654, 12345, 87878.

Now, What will be the fastest way to return all the values from the array that exist in the SQL server table? (So it should return only this: ("12345"))

Thank you Very Much!!

share|improve this question

marked as duplicate by Michael Berkowski, AD7six, hohner, g.d.d.c, Lukas Knuth Feb 21 '13 at 0:33

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.

1  
What have you tried? See ask advice, please. –  John Conde Feb 20 '13 at 16:09
    
w3schools.com/sql/sql_in.asp –  Misch Feb 20 '13 at 16:11
    
use IN clause in your sql, set into it implode(',', $numbers) –  sanj Feb 20 '13 at 16:13
    
@Misch please see w3fools.com –  Lukas Knuth Feb 21 '13 at 0:33
    
@LukasKnuth thanks for the info, I always thought of w3schools being an official W3C website. I just used the first result for the google search "sql in". –  Misch Feb 21 '13 at 10:40

1 Answer 1

up vote 0 down vote accepted

After some more search i have found the answer, Sorry for that:

$ids = join(',',$numbers);  
$sql = "SELECT * FROM phones WHERE TNumbers IN ($ids)";

Thank you anyway!

share|improve this answer
    
this is prone to SQL injections, so make sure that your $numbers array doesn't contain malicious code! –  Misch Feb 20 '13 at 16:33

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