Facebook Query Language FQL tips and tricks

Share on TwitterDigg This

The FQL object enables running FQL queries using the Graph API. Facebook Query Language, or FQL, enables you to use a SQL-style interface to query the data exposed by the Graph API. It provides for some advanced features not available in the Graph API, including batching multiple queries into a single call.

You can execute FQL queries by fetching https://api.facebook.com/method/fql.query?query=QUERY. You can specify a response format as either XML or JSON with the format query parameter.

Queries are of the form SELECT [fields] FROM [table] WHERE [conditions]. Unlike SQL, the FQL FROM clause can contain only a single table. You can use the IN keyword in SELECT or WHERE clauses to do subqueries, but the subqueries cannot reference variables in the outer query’s scope. Your query must also be indexable, meaning that it queries properties that are marked as indexable in the documentation below.

For any query that takes a uid, you can pass me() to return the logged-in user. For example:

FQL to get details of the current logged in user by me() function

SELECT name FROM user WHERE uid = me() 
SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Fql to get album id and details by album name

function get_album_id_by_album_name($album_name){//return array of data
        $fql            =   'SELECT aid, owner, name, object_id FROM album WHERE owner=me() and name="'.$album_name.'"';
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $this->facebook->api($param);
	return $fqlResult;
}

FQL to get facebook user profile details

function get_user_profile_details($user_facebook_id){
 
		$fql    =   "SELECT name,pic_square,pic,uid 
FROM user  where	
 uid  =".$user_facebook_id;
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

FQL to check the user is facebook application user

function check_is_app_user($facebook_user_id){//return 1 if the user is using the current application
	try{
	$user=$facebook_user_id;
        $fql    =   "select is_app_user from user where uid=" . $user;
            $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
 
            $fqlResult   =   $this->facebook->api($param);
 
	if($fqlResult)
		return $fqlResult['0']['is_app_user'];
       else
                return 0;
        }
	 catch(Exception $o){
            d($o);
        }
 
}

FQL to get the tagged photos of facebook user and tag details,tag co-ordinates of user photos

function get_user_tagged_photos_details($user_facebook_id){
 
		$fql    =   "SELECT pid,text,xcoord,ycoord,created FROM photo_tag WHERE subject =".$user_facebook_id;
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}
function get_tagged_users_id_by_photoid($photo_id){
 
		$fql    =   "SELECT text FROM photo_tag WHERE pid =".$photo_id;
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

pid-The ID of the photo being queried.
xcoord-The center of the tag’s horizontal position, measured as a floating-point percentage from 0 to 100, from the left edge of the photo.
ycoord-The center of the tag’s vertical position, measured as a floating-point percentage from 0 to 100, from the top edge of the photo.

FQL query to get user facebook notification filter by unread

function get_fb_notification_by_uid($user_id){
 
		$fql    =   "SELECT notification_id, sender_id,app_id,icon_url, title_html, body_html, href
FROM notification
WHERE recipient_id=".$user_id."
AND is_unread = 1
AND is_hidden = 0";
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

sender_id-The user ID of the sender of the notification.
href-The URL associated with the notification. This is usually a location where the user can interact with the subject of the notification.
app_id-The ID of the application associated with the notification. This may be a third-party application or a Facebook application (for example, Wall).
is_unread-Indicates whether the notification has been marked as read. Use notifications.markRead to mark a notification as read.

FQL to get photos of my friends posted by date or week

function get_friends_photo_details($date){
 
		$fql    =   "SELECT pid, caption, aid, owner, link, src_big, src_small, created, modified FROM photo WHERE aid IN 
   (SELECT aid FROM album WHERE owner IN 
      (SELECT uid2 FROM friend WHERE uid1=me()) 
   ) 
AND created ."$date;
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}
 
function get_friends_photo_details_last_week(){
 
		$fql    =   "SELECT pid, caption, aid, owner, link, src_big, src_small, created, modified FROM photo WHERE aid IN 
   (SELECT aid FROM album WHERE owner IN 
      (SELECT uid2 FROM friend WHERE uid1=me()) 
   ) 
AND created > One_week_ago_in_unix_time ORDER BY created DESC";
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql
 
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult[0];
}

FQL to get photos by facebook albums

function get_photos_by_album_id($album_id){
 
if($album_id)
        $fql            =   'SELECT pid,src_big,owner,link,position,created,caption,src FROM photo WHERE aid="'.$album_id.'" ORDER BY created DESC LIMIT 0,6';
 
                $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $this->facebook->api($param);
		return $fqlResult;
 
}

Related

About prabin t p

Connect with me on Google+
This entry was posted in facebook and tagged , , . Bookmark the permalink.

14 Responses to Facebook Query Language FQL tips and tricks

  1. Rashed says:

    Hi,
    I have already read your all FQL tutorial.it’s a great and awesome tutorial.But I am facing one problem .which place I will be put FQL script. please describe this

    • Myros says:

      Hi,I have already read your all FQL triautol.it’s a great and awesome triautol.But I am facing one problem .which place I will be put FQL script. please describe this

      • Katherine says:

        Hi Dutch,That update does not. (No one asked for it yet and while I try (at the risk of ever exndaping scope) no way do I think of every thing up front, so it is really good if people give feedback and put some requests in.Now it is on the list and may make it into the next update.Thanks for noting it.

    • Nagesh says:

      Assuming MySQL from tag, even though OP makes no meniotn of it.You should edit your question and add the fact that you are conducting order by operations as well (from a comment you posted to a solution). order by operations will also slow down queries (as will various other mysql ops) because MySQL has to create a temp table to accomplish the ordered result set (more info ). A lot of times, if the dataset allows it, I will pull the data I need, then order it at the application layer to avoid this penalty.Your best bet is to EXPLAIN your most used queries, and check your slow query log.

      • Elisangela says:

        If one additional query per page is rellay killing you that bad, then query caching is the wrong way to fix that. Query caching uses textfiles, and uses disk I/O which is -really- slow. Perhaps using Redis or Memcached would be much better suited for this task.

  2. Abe says:

    Assuming MySQL from tag, even though OP makes no mtineon of it.You should edit your question and add the fact that you are conducting order by operations as well (from a comment you posted to a solution). order by operations will also slow down queries (as will various other mysql ops) because MySQL has to create a temp table to accomplish the ordered result set (more info ). A lot of times, if the dataset allows it, I will pull the data I need, then order it at the application layer to avoid this penalty.Your best bet is to EXPLAIN your most used queries, and check your slow query log.

  3. Aby says:

    Assuming MySQL from tag, even though OP makes no minoetn of it.You should edit your question and add the fact that you are conducting order by operations as well (from a comment you posted to a solution). order by operations will also slow down queries (as will various other mysql ops) because MySQL has to create a temp table to accomplish the ordered result set (more info ). A lot of times, if the dataset allows it, I will pull the data I need, then order it at the application layer to avoid this penalty.Your best bet is to EXPLAIN your most used queries, and check your slow query log.

    • Hernan says:

      Hi, great tutorial!I have a prolbem, i’ve downloaded your project and merge with mine.Everything seems to work OK, when i enter to mydomain.com/user/It redirects me to facebook, and ask me for the app’s permission.I click Allow and it redirects me to mydomain.com/facebook?state= ..But, i don’t know why, at the facebook method:if (!$this->fb_connect->user_id) { echo not allowed’;}else{ echo allowed’;}it shows not allowed , but i allowed the application.My facebook config is ok, but i don’t know if i have to configure something on my facebook application.I’ll be very gratefull if you can help me.

  4. Kingshu says:

    Hi, I am trying to get the comments on my friends’ profile pictures using FQL, but this query is returning empty array(“data” => [ ] )

    select comment_info from photo where owner = ‘$friends_uid_here’

    I have given all permissions in the access token. The result is the same in both the program and Graph API Explorer.

  5. Maheedhar says:

    Hi Prabin,

    Good tutorial, need some help in one FQL.

    I have been trying to check with all the links available in the internet to give me the posts and details of the users for a particular product page (here it is “fitbit”)

    Can some one give me a query to fetch the data from https://www.facebook.com/fitbit?fref=ts page, my basic need is to get the updates and user comments from the page and use it for sentiment analysis. If you could redirect me to some links which can help me with the Sentiment analysis of a product through Facebook data it’ll be a great help for me.

    Thanks in advance,

  6. asma says:

    I would like to ask if it is possible to get my friends real time latitude and longitude using FQL Queries.

  7. ranga says:

    need help, how to get friends location and work information in fql

    ex- location=US and work=apple.

    also is it possible to get the information, people who are not in my friend list.

  8. Chea Samnang says:

    Why can only get data from first id only?

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

*