Although the FQL language isn’t ANSI-SQL complete, it does have some simple operators and functions to help you work with user data. FQL has boolean operators (AND, OR, and NOT), comparison operators (=, >, >=, <, <=, <>), and arithmetic operators (+, -, *, and /). The functions included in FQL, although not exhaustive, allow you to perform some basic string manipulations. To conserve bandwidth, you can use the concat() function to group several tuples together:

<?php

$facebook_config['debug'] = false;

$facebook_config['api_key'] = '<your_api_key>';

$facebook_config['secret_key'] = '<your_secret_key>';

require_once('<path_to_client_library>/facebook.php');

$facebook = new Facebook($facebook_config['api_key'],

$facebook_config['secret']);

/**

* Ensure the user has logged on to Facebook

*/

$user = $facebook->require_login();

/**

* Construct the FQL request

*/

$fql = "SELECT concat(first_name, ' ', last_name)

FROM user

WHERE uid = '$user'";

/**

* Pass the FQL to Facebook through the API client

*/

$fql_result = $facebook->api_client->fql_query($fql);

/**

* Print the results to the screen

*/

echo "<pre>FQL Result:" . print_r($fql_result, true) .

"</pre>";

?>

The previous example simply selects the first and last names of the user who is currently making the request. The resulting page will display an array in the following format:

FQL Result:Array

(

[0] => Array

(

[anon] => <your_first_name> <your_last_name>

)

)

You might be saying to yourself, “This is pretty useless. What’s the difference between this and just calling both the fields?” Well, if you have any bandwidth concerns, you can alleviate some of those issues by using the concat function to put fields that you need together. For instance, you might want to put a specific string into your page that combines several fields in a specific way. Letting the Facebook servers do some of this processing before it gets back to your server will not only decrease your server load but can also cut down on your bandwidth in order to speed up your application.


Not only can you do simple SQL-style selects, but you can also perform subqueries. Take, for example, this FQL equivalent for the facebook.events.get REST API call:

SELECT eid, name, tagline, nid, pic, pic_big, pic_small, host, description,

event_type, event_subtype, start_time, end_time, creator, update_time,

location, venue

FROM event

WHERE eid IN (SELECT eid FROM event_member

WHERE uid=uid AND rsvp_status=rsvp_status) AND

eid IN (eids) AND

end_time >= start_time AND

start_time < end_time

I won’t go into the theory behind nested queries, but I will mention that they are very useful for testing set membership, set comparisons, and set cardinality. And, this expansion of the REST call serves as a good example for writing your own custom FQL expressions.

You might find that you will need to take some additional processing to make sure your information is displayed in a specific order. You’ll have to do this with your client language. For PHP, you need to sort the array and then slice it. Let’s take the following:

<?php

$fql = "SELECT eid, name, location

FROM event

WHERE eid IN (

SELECT eid

FROM event_member

WHERE uid = '$user'

)";

$fql_result = $facebook->api_client->fql_query($fql);

asort($fql_result);

array_slice($fql_result, 0, 5);

?>


The previous passes an FQL query to find events for the current user, sorts the resulting PHP array, and returns the array with the six elements (positions 0–5) of the query result.

FQL allows you as a developer to have granular control of the information that you retrieve about your users from the Facebook servers. Although it’s not as robust as you might sometimes need (or want) it to be, you can generally get around FQL’s limitations with some post-processing in the language in which you’re developing. Additionally, as the complexity of your FQL increases with subqueries, you might at some point run into problems. As I’ve mentioned earlier, using the Facebook API Test Console at http://developer.facebook.com/tools.php is a great place to help debug your code. For instance, if you take the previous query and take out the WHERE clause so that your FQL statement reads as follows:

SELECT eid, name, location

FROM event

WHERE eid IN (

SELECT eid

FROM event_member

)
then, when executed, this will raise an error because you must have a limiting WHERE clause. If you missed it when you look at your code, the resulting XML response shows an error code of 601 and an error message of “Parser error: WHERE clause is required.” Fortunately, this is an easy fix, but you might find yourself working with more complicated interactions with FBML and FQL, and this tool can provide invaluable help in discerning where your bugs exist.

0 comments