Facebook Query Language

Working with Facebook API Part-3: Facebook Query Language (FQL)


The Facebook through FQL, enables you to query data from the Graph API using the near to SQL style known. In this article we will learn how to perform, prepare and test queries on the Facebook (FQL) platform using the SDK for PHP or Javascript.

Let’s quote some pretty interesting examples of FQL as well as how to use the tool support for developers called Graph API Explorer provided by facebook to develop and test FQL queries

We will also learn about Leadpages Alternatives, more deeply into online ad campaigns which are being directed to the landing pages the web visitors could arrive. Landing pages can be used with Facebook Lead ads as well. It may sound confusing but there are easy alternative ways on how to do it. See for Best Leadpages Alternatives.


In this topic we will learn how to perform an FQL both through the PHP SDK and the JAVASCRIPT SDK . As support, we use the following FQL query:

(Simply query the amount of friends you have facebook)


Running through PHP:

Example of how to run FQL through the PHP SDK:

 $result  = $facebook ->api(array(

"method"  => "fql.query" ,

"query"   => " SELECT friend_count FROM user WHERE uid =me()" ,


if($result) {

echo  "Number of friends: {$result [0]['friend_count']}" ;


See Full Functional PHP Example Below:

Complete working example: (Just only parameterize the appId and secret)


require  'facebook-php-sdk-master/src/facebook.php' ;

// Create the instance of the application, stating the appid and secret

$facebook = new Facebook(array(

'appId' => '236076976577688',

'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',


// Get the id of the User

$user = $facebook->getUser();

if ($user){ // User logged

try {

// Get User data logged

$user_profile  = $facebook->api('/me');


"method"=>"fql.query" ,

"query"=>"SELECT friend_count FROM user WHERE uid =me()",


if($result) {

echo  "Number of friends: {$result[0]['friend_count']}" ;


} catch(FacebookApiException $e ) {

echo "<pre>";

echo "</pre>";
$user  = null;


} else  {

// User not logged in, request authentication

$loginUrl  = $facebook->getLoginUrl();

header ( "Location:$loginUrl");


 Output:  Number of friends: 1000 (Your  total friends)

Running through Javascript:

Example of how to run FQL via Javascript SDK:


FB.api ({

method: 'fql.query',

query:"SELECT friend_count FROM user WHERE uid =me()"

}, function (resp) {

if(resp.length) {

alert (resp[0][friend_count]);




See Full Functional JAVASCRIPT Example Below:

<!doctype html>



<meta charset="utf-8">

<title>Facebook API</title>

<script src="http://connect.facebook.net/pt_BR/all.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>



appId : '236076976577688',

status: true, cookie: false, xfbml: false


var FBCountFriends = function() {


method: 'fql.query',

query:  'SELECT friend_count FROM user WHERE uid =me()'

}, function(resp) {

if (resp.length) {

alert("Your Total Friends "+resp[0].friend_count);




// onload

jQuery(document).ready(function() {

FB.getLoginStatus(function(response) {

if (response.status === 'connected') {


} else {

FB.login(function(response) {

if (response.authResponse) {












Output: Your Total Friends: 1000

Testing FQL queries using the Graph API Explorer

Facebook provides a tool to support developers very helpful, called Graph API Explorer . Through it you can test and develop their FQLs. Through this tool you can enable / disable permissions online form during testing of FQL. Recalling that access to some information need user permission so that your application can access them.

To test / debug their FQLs, open the Graph Explorer and click the FQL Query . Before inserting / draw your FQL, click the Get Access Token and select the permissions your app needs to request that your FQL reach the goal of getting the information you need.


Testing FQL queries using the Graph API Explorer

Then select the desired permissions. See the figure below, we choose you to perform some interesting some nice permissions tests: (mail, user_likes, user_photos, user_about_me, user_birthday, user_games_activity).

Remember that access to some information need certain permissions from the user. For example, to access the photos and albums published by the user, you will need it to approve permissionuser_photos .

For a complete reference on the possible permissions, access the link:http://developers.facebook.com/docs/reference/login/

Facebook Api Permissions

After clicking the Get Access Token you’re already able to test their FQLs. See example below:

Get Facebook API Access Token

Interesting examples of FQL

Selected below some examples of FQL so you can get an idea of what is possible. Depending on the information it needs, advise you to consult the link that contains the reference of tables to query the Graph API or FQL ready to look now by clicking on google. Let’s look at examples:

  • Select all the user’s friends who are using your application:

SELECT  uid, name  FROM  user  WHERE  uid IN ( SELECT  UID2 FROM  friend WHERE  uid1 = me ()) AND  is_app_user = 1


  • Checking if certain user likes fan page:

SELECT uid FROM page_fan WHERE page_id = page_id AND uid = me()

--- Page_id change the id of the fanpage

  • Getting all images owned by the user:

SELECT  src, caption FROM  photo WHERE  owner = me()

  • Select id, name and picture of their friends, limiting the result in 25 records:

SELECT  uid, name , pic_square FROM  user  WHERE  uid IN  ( SELECT  UID2 FROM  friend WHERE  uid1 = me () LIMIT 25)

  • Gets details of user albums:

SELECT  aid, owner, name , object_id FROM  album WHERE  owner = me()

  • Gets user notifications that have not been read

SELECT notification_id, sender_id, app_id, icon_url, title_html, body_html, href

FROM notification WHERE recipient_id = me () AND is_unread = 1 AND 0 = is_hidden

Limitations of FQL

Selected below some limitations present in FQL and that usually generate questions for developers who are starting to develop FQLs:

  • Unable to create queries with JOINs (An alternative is to use sub-selects)
  • * Can not Use: SELECT * (You have to specify all the columns you want to get the table)
  • You can not use basic functions like sum () , count () , etc..

 Working with Facebook API Part-3: Facebook Query Language (FQL)

One comment to “Working with Facebook API Part-3: Facebook Query Language (FQL)”

Leave a Reply

Your email address will not be published. Required fields are marked *