Calling FQL with the Facebook

Advantages Of FQL Over The Facebook API Methods

The data-gathering capabilities of FQL are identical to many of the API calls. However, before dismissing FQL as redundant, it is important to recognize that FQL does have some notable advantages over API access to Facebook, including the following:

  1. FQL is more efficient. When you retrieve data using the API, you bring back all of the field data for a given record. However, with effective use of the select clause in an FQL statement, you can specify only those specific fields you want to have included in the result set. In fact, FQL does not even support selecting all records in a table with select * statements.

  2. FQL can reduce the number of server requests for complex requests. When you are retrieving data, you often find yourself making an initial request, working with the results, and then making a second request for more specific information. For instance, suppose I need to get the name and status of all members attending an event. Using the API, I would call events.getMembers to get all members attending a specific event. Next, I would take that array of members and use users.getInfo to get the name and status for each of them. In contrast, with FQL, these two requests can be done inside a single complex query (using the in clause for a subquery).

  3. FQL is Web-language neutral. The Facebook API has several versions, each of which is written for a particular programming language, such as PHP, Java, or Ruby on Rails. Therefore, if you work in multiple programming environments, your Facebook code must be specific to each one. In contrast, because FQL is programming-language independent, you can use the same FQL statements anywhere.

The API calls are actually wrappers around lower-level FQL calls. In the end, even if you prefer to spend most of your time working with the API, you may find specific occasions in which you want to take advantage of the efficiency of FQL.

Making An FQL Statement

To make an FQL query, use the fql.query API method. Its syntax in PHP looks like this:

$result_set = $facebook->api_client->fql_query("select name from user where uid=634164579");

The fql_query method sends the FQL query string to Facebook for processing. The results are returned to the PHP client as an array. Note that you can also return results in XML and JSON format as well for other clients. The array result set you’d get back in PHP would be:

	[0] => Array
	    [name] => Mert TOL

You can then work with the result set just like you would with an array obtained from a Facebook API call. And note that because Facebook manages the database connections for you, you do not need to open and close databases as part of your application.

Differences Between SQL and FQL

Although FQL is based on SQL syntax, the two are not the same. SQL is designed to be a flexible query language in a variety of contexts. FQL, on the other hand, is a query language for a specific, very targeted data set.

Therefore, FQL has some limitations compared to SQL that you need to be aware of, particularly if you already know SQL. These include the following:

  1. select * is not allowed. You need to specify all the fields by name in which you want to include in the result set.

  2. The from clause can only include a single table.

  3. At least one field in the where clause must be classified as indexable.

  4. join is not supported (though in subqueries are).

  5. The groupby, order by, count, and limit keywords are not supported.

  6. The between and like operators are not supported.

  7. Because you have read-only access, you obviously cannot use keywords like update, delete, insert into, or create table.

There have been no comments | Subscribe to Comments | Jump to Form »

Post Comment on This Article

Your e-mail address won't be published. If you simply add some value to the original post and stay on the topic, your comment will be approved.

You can use Textile parameters on your comments. For example: _italic_ *bold* bq. quated text "link text":URL — Get your own picture next to your comment with a Gravatar account.