Advice on checking an array against a database, please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mendingo
Forum Commoner
Posts: 28
Joined: Sun May 23, 2004 1:27 pm

Advice on checking an array against a database, please.

Post by mendingo »

I have an array, array1 of words.

I want to return an array, array2 that contains all the words from array1 that do not exist in a given column (col1) of a given table (Table1).

What's the most efficient way of doing this?
evanz
Forum Newbie
Posts: 6
Joined: Wed May 19, 2004 8:44 am

Post by evanz »

can use array_diff($ar1,$ar2).
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$array1 = array('some','words','here');
foreach($array1 as $key => $value) 
  $array1[$key] = mysql_escape_string($value);
$res = mysql_query('select word from sometable where word not in("' . implode('","', $array1) . '")');
while( list($word) = mysql_fetch_row($res) ) {
  // do something to word
}
Post Reply