2
//get all id's of ur friend that has installed your application
 $friend_pics=$facebook->api( array( 'method' => 'fql.query', 'query' => "SELECT uid FROM user WHERE uid   IN(SELECT uid2 from friend WHERE uid1='$user') AND is_app_user = 1" ) ); // this query work fine
//your top10 friends in app
$result="SELECT * FROM fb_user WHERE user_id IN($friend_pics) ORDER BY oldscore DESC LIMIT 0,10";
db_execute($result);

i want to retrive ten top scorer from my database stored in oldscore but in my second query the array name $friend_pics is not working i guess,plz help me thanks

Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
  • You'll need to post more of your code, and / or your db schema. There could be a lot of problems here. This isn't enough information. – bonez Mar 06 '11 at 00:10
  • Hopefully one of the placeholder-enabled access methods (mysqli or pdo) support a construct like this (they should generally be used anyway). –  Mar 06 '11 at 00:17

3 Answers3

3

There is no native support for that. Even the bound parameter APIs do not allow to use arrays for IN clauses. You have to construct the query with some helper code:

$friend_pics = array_map("mysql_real_escape_string", $friend_pics);
$friend_pics = "'" . implode("', '", $friend_pics) . "'";
"SELECT * WHERE user_id IN ($friend_pics)              "

A simpler alternative would be mysqls FIND_IN_SET() however:

$friend_pics = mysql_real_escape_string(implode(",", $friend_pics));
"SELECT * FROM fb_user WHERE find_in_set(user_id,'$friend_pics')     "
mario
  • 144,265
  • 20
  • 237
  • 291
1

Why don't you convert that array into a string of long list of your values unless array is huge size.

    $ids    = array(2, 4, 6, 8);
$params = implode(",", array_fill(0, count($ids), "?"));

    $result="SELECT * FROM fb_user WHERE user_id IN($params) ORDER BY oldscore DESC LIMIT 0,10";
db_execute($result);
yogsma
  • 10,142
  • 31
  • 97
  • 154
1

$friend_pics should be a string containing a comma separated list i.e. "1, 2, 3, 4". If it is not, then your second query will always fail.

  • this work perfectly$friend_pics=$facebook->api( array( 'method' => 'fql.query', 'query' => "SELECT uid FROM user WHERE uid IN(SELECT uid2 from friend WHERE uid1='$user') AND is_app_user = 1" ) );foreach($friend_pics as $status_a) { $status_sql[] = '\''.$status_a['uid'].'\''; } $status = implode(',',$status_sql);$result= "SELECT * FROM fb_user WHERE user_id IN($status) ORDER BY oldscore DESC LIMIT 0,10"; db_execute($result); this is damn rite thaks 4 ur help – Fawad Ghafoor Mar 06 '11 at 23:26