FB.Data.query

From Facebook Developer Wiki (FbDevWiki.com)
Jump to: navigation, search

Performs a parameterized FQL query and returns a FB.Data.query object which can be waited on for the asynchronously fetched data.

Note: According to the Developer Roadmap, FB.Data.* was deprecated for new apps on January 1st, 2012. Use FB.api instead

Examples

Make a simple FQL call and handle the results.

 var query = FB.Data.query('select name, uid from user where uid={0}', user_id);
 query.wait(function(rows) {
   document.getElementById('name').innerHTML =
     'Your name is ' + rows[0].name;
 });

Display the names and events of 10 random friends. This can't be done using a simple FQL query because you need more than one field from more than one table, so we use FB.Data.query to help construct the call to fql.multiquery.

 // First, get ten of the logged-in user's friends and the events they
 // are attending. In this query, the argument is just an int value
 // (the logged-in user id). Note, we are not firing the query yet.
 var query = FB.Data.query(
       "select uid, eid from event_member "
     + "where uid in "
     + "(select uid2 from friend where uid1 = {0}"
     + " order by rand() limit 10)",
     user_id);
 
 // Now, construct two dependent queries - one each to get the
 // names of the friends and the events referenced
 var friends = FB.Data.query(
       "select uid, name from user where uid in "
     + "(select uid from {0})", query);
 var events = FB.Data.query(
       "select eid, name from event where eid in "
     + " (select eid from {0})", query);
 
 // Now, register a callback which will execute once all three
 // queries return with data
 FB.Data.waitOn([query, friends, events], function() {
   // build a map of eid, uid to name
   var eventNames = friendNames = {};
   FB.Array.forEach(events.value, function(row) {
     eventNames[row.eid] = row.name;
   });
   FB.Array.forEach(friends.value, function(row) {
     friendNames[row.uid] = row.name;
   });
 
   // now display all the results
   var html = '';
   FB.Array.forEach(query.value, function(row) {
     html += '<p>'
       + friendNames[row.uid]
       + ' is attending '
       + eventNames[row.eid]
       + '</p>';
   });
   document.getElementById('display').innerHTML = html;
 });

Parameters

Name Type Description
template String FQL query string template. It can contains optional formatted parameters in the format of '{}'.
data Object optional 0-n arguments of data. The arguments can be either real data (String or Integer) or an FB.Data.query object from a previous FB.Data.query.

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Graph API
FQL
Toolbox