Must use FQL Multiquery to retrieve data


fb_logoIf you are developing facebook application or facebook connect base application, then you might know how to retrieve data from FQL tables.

To retrieve data from FQL tables facebook provides two api methods, fql_query() and fql_multiquery()

Using both functions, you can evaluate fql queries, but if you need to evaluate a series of queries then remember, you should use fql_multiquery()

syntax and usage of fql_multiquery:

$query1         =   "SELECT uid, name from user where uid in ($friends)";
$query2         =   "SELECT pid, subject, text from photo_tag where pid in ($pids)";

$queries        =   '{
                       "query1": "' . $query1 . '",
                       "query2": "' . $query2 . '"
                     }';
$result  = $facebook->api_client->fql_multiquery($queries);

$userInfo       =   $result[0]['fql_result_set'];
$photoTags      =   $result[1]['fql_result_set'];

Advantage of using fql_multiquery:

  1. Multiple queries in one call
  2. Fetch data from one query and use it in another query within the same call
  3. Better performance than running a series of fql.query calls with batch.run.

For point 2, think you may want to get some data about users who are attending an event. Then at first you have to retrieve uids for that event and then using another fql call you have to retrieve user data. Now here you’ll see how you can integrate this solution using fql_multiquery()

$query1 = "SELECT uid, rsvp_status FROM event_member WHERE eid=$eventId";
$query2 = "SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)";

$queries        =   '{
                       "query1": "' . $query1 . '",
                       "query2": "' . $query2 . '"
                     }';
$result  = $facebook->api_client->fql_multiquery($queries);

Look in query2, you are referencing the result of query1 by #query1
To learn more about FQL and FQL Queries visit:

You can fetch data from one query and use it in another query within the same call
mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

14 Comments

  • Marilu Ojima
    January 30, 2010 at 7:38 am

    Hi. Very interesting Post. Not really what i have searched over Google, but thanks for the information.

  • Mariam Rhodarmer
    February 4, 2010 at 2:39 am

    Hello there. Thank you. I check it regularly to get the newest stuff.

  • Raheel Hasan
    March 10, 2010 at 12:51 am

    What about the major disadvantage: If one query fails, both will fail. So you cant do individual exception handling for each query with this method.

    So I suggest, we use it only for complex queries as in example 2, and not just to save connection calls.

  • Kyli
    August 18, 2010 at 7:05 pm

    FYI, your site is the only place where I can get enlighted,
    Thanks so much.

  • Kenny Faaita
    February 6, 2011 at 1:27 am

    I am not rattling wonderful with English but I get hold this real easygoing to interpret .

  • Agnus Darron
    February 6, 2011 at 8:54 am

    I really like reading through and I think this website got some genuinely useful stuff on it! .

  • Johnny
    March 2, 2011 at 11:39 pm

    Awesome site, Mahmud!

    Question: Should I use multi_query() to fetch fan counts for about 2000+ pages? If not, what’s the best way?

    Thank you!

    • mahmud ahsan
      mahmud ahsan
      March 3, 2011 at 11:50 am

      Hello Johnny, this post is written in long ago and facebook changed their api system introduced graph api. So use graph api.

      • Johnny
        March 4, 2011 at 1:22 am

        thanks, mahmud! i figured it out the hard way.

        Make more tutorials!! 🙂

  • Sundar
    March 16, 2011 at 6:27 am

    When I tried to use above example I got this error…

    “Fatal error: Call to a member function fql_multiquery()”

    My coding is
    —————————————————-
    $queries=”{
    ‘query1′:’Select flid,uid From friendlist_member Where flid IN (Select flid From friendlist Where owner=me())’,
    ‘query2′:’Select flid,uid From user Where flid IN (Select flid From friendlist Where owner=me())’
    }”;
    //Select flid,uid From friendlist_member Where flid IN (Select flid From friendlist Where owner=me())
    $fb_res_frdlist_members=$fb->api_client->fql_multiquery($queries);
    echo ”;
    print_r($fb_res_frdlist_members);
    echo ”;
    I am using facebook php file(const VERSION = ‘2.0.4’;) but it contains no function called api_client

    Can you explain what I am missing?

    • Sundar
      March 16, 2011 at 6:28 am

      Just now I studied your above comments, can you give some tutorial link to do in graph api plz…

    • Sundar
      March 16, 2011 at 6:40 am

      The following one worked for me…
      I am posting here coz it might help someone. Its using rest api call method…

      $param=array(
      ‘method’=>’fql.multiquery’,
      ‘queries’=>”{
      ‘query1′:’Select flid,uid From friendlist_member Where flid IN (Select flid From friendlist Where owner=me())’,
      ‘query2′:’Select uid From user Where uid=me()’
      }”,
      ‘callback’=>”,
      );
      //Select flid,uid From friendlist_member Where flid IN (Select flid From friendlist Where owner=me())
      //$fb_res_frdlist_members=$fb->api($param);
      $fb_res_frdlist_members=$fb->api($param);
      echo ”;
      print_r($fb_res_frdlist_members);
      echo ”;

  • Saweed Hassan
    November 16, 2012 at 12:32 am

    Hi

    I want to get post/status of fanpage and all its comments

  • vonu
    January 22, 2013 at 7:16 pm

    is there any way to get friends album list and photos of there albums.
    or have set some permission for that.

    anyone please tell me the whole process for getting friends album and there pictures.

    please please help me..

Comments are closed here.

About Me

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

Flickr

Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed
Flickr Feed

Twitter Feed

Fanpage