Facebook Cookbook - Jay Goldman [131]
App Friends
Problem
I need to find all of the friends of a specific user who have already installed my application.
Solution
SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT
uid2 FROM friend WHERE uid1 = $user);
Discussion
This particularly comes in handy when you’re displaying an fb:multi-friend-selector control in FBML and want to exclude friends of the current loggedinuser who have already installed your app (see Invitations and Requests).
Birthday Friends
Problem
I need to find all of a specific user’s friends whose birthdays are on a given day using FQL.
Solution
SELECT uid FROM user WHERE strpos(birthday, "September 27")
= 0 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $uid)
Discussion
Rather than supporting the LIKE comparator from SQL, Facebook has included the ability to run a small set of PHP-like functions in your queries (see Functions and Operators). We’re using the strpos() function here, which returns the position of the needle in the haystack (the first parameter—birthday—is known as the haystack, and the second parameter—“September 27”—is known as the needle). Since we’re looking for people who share the same birthday and we don’t care about the year, we want to return results where the string starts with “September 27”, which would mean it’s at position 0. In more traditional SQL, this query would have:
SELECT uid FROM user WHERE birthday LIKE "September 27%"
AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $uid)
You can, of course, substitute any day you’d like for “September 27”, which just happens to be my birthday (please email cakes and other goodies). This query comes from the excellent Facebook Developers Wiki Sample FQL Queries page, which you’ll find at http://wiki.developers.facebook.com/index.php/Sample_FQL_Queries.
600 Errors
Problem
My FQL doesn’t work! I’m getting a 600 error!
Solution
Don’t panic! 600-class errors are Facebook’s way of telling you that there’s something wrong with your FQL query. The possible error codes are listed in Table 8-19.
Table 8-19. FQL error codes
Error code
Description
601
Error while parsing the FQL statement.
602
The field you requested does not exist.
603
The table you requested does not exist.
604
Your statement is not indexable.
605
The function you called does not exist.
606
Wrong number of arguments passed into the function.
Discussion
Pay special attention to 604 errors, because they often have extra information in them about why the statement isn’t indexable. As an example, metrics table queries are considered indexable only if the date range you’re querying on isn’t bigger than 30 days. If it is bigger, you’ll get a 604 error, which tells you that the range is too big.
If you’re getting an error and are having trouble tracking it down in the context of your code, try extracting the query and running it directly inside the API Test Console at http://developers.facebook.com/tools.php?api. If you select “fql.query” as the Method, you can play with FQL and see live results on the righthand side.
Preload FQL
Problem
I want to increase the performance of my pages that require the results of an FQL to do their initial rendering.
Solution and Discussion
Facebook has developed a preload FQL system that enables you to have Facebook send along the results of a specific FQL query when it requests FBML pages from your server. Since this is more reliant on an API call than on FQL, it’s documented in the Admin.SetAppProperties() API method (see Chapter 9). More information can also be found in the Developers Wiki at http://wiki.developers.facebook.com/index.php/Preload_FQL.
Chapter 9. Facebook API
You’ve done a bang-up job of planning, your brilliant, unpolished diamond is starting to shine with a beautiful design, you have stellar FBML built out on a standards-compliant framework, your FBJS screams Ajax cleanliness from 1,000 feet away, and you’ve mastered FQL queries like the back of your normalized