1000 queries vs where userid='1' or userid='2',

Each user have a list of friends from Facebook. I need to check if any of them is already a member of our site.

I get a list of FB friends in array. Is there any better way than doing the following?


foreach($friends as $key => $value){
  $results=mysql_query("SELECT * FROM `users` WHERE userId='$value'") or die (mysql_error());
}

or another way:


$where="userId='435345434'";
foreach($friends as $key => $value){
  $where=$where." or userId='".$value."'";
}
$results=mysql_query("SELECT * FROM `users` WHERE $where") or die (mysql_error()); 

I hope anybody can help with this.

tnx!

Set up a comma separated list of the userids and then do a single SQL call using IN.

… and make sure you don’t exceed the MySQL query limit (length of the query in characters).
You could for example pose a query for every 500 users or so.


$f = array();
foreach($friends as $friend)
{
   $f[] = $friend;
   if (count($f) == 500)
   {
      // query
      $f = array();
   }
}
if (count($f) > 0)
{
  // query
}

something like that

tnx, that works great!