Facebook Cookbook - Jay Goldman [117]
Album Table
Problem
What’s the schema for the album table?
Solution
The album table holds all of the photo albums that users have created in the Facebook Photos application. Its fields are listed in Table 8-2. Queries to this table will only return data the current user is allowed to see (in other words, you can’t request albums that this user isn’t allowed to see). More information about this table, including an up-to-date listing of fields, can be found at http://wiki.developers.facebook.com/index.php/Album_(FQL).
Table 8-2. album table fields
Name
Type
Index
Description
aid
int
•
Album ID.
cover_pid
int
•
pid (photo ID) of the photo that has been set as the cover of the album.
owner
int
•
uid (user ID) of the owner of this album (i.e., the user who created it).
name
string
Name of the album.
created
string
Creation date of the album.
modified
string
Date that the album was last updated, in epoch seconds (see Formatting Relative Time for an explanation of epoch seconds).
description
string
Description entered by the album’s owner.
location
string
Location entered by the album’s owner (e.g., Toronto, The Moon).
size
int
The number of photos in the album.
link
string
URL to the album (i.e., the actual Facebook URL for this album).
Note that only the fields marked as “Index” in this table can be used in an FQL query’s WHERE clause, but any of the fields can appear in the SELECT.
Discussion
If you’d rather use the API to access albums, try the Photos.getAlbums() method.
Retrieving an Album
Problem
I need to retrieve a Facebook album using FQL.
Solution
This is your most basic album-related query:
SELECT aid, cover_pid, name, link FROM album WHERE owner = $uid;
Discussion
Since SELECT * isn’t supported in FQL, you’ll have to list out the actual fields you’re looking for. This would be enough to let you display someone’s albums with links back to the original. Remember that this query will only return albums that the current loggedinuser has permission to see.
Counting All of a User’s Photos
Problem
I need to use FQL to count all of the photos that a user has uploaded to the Facebook Photos app.
Solution
This would be easier in real SQL, since you could use the sum() function to add up all the photos. We’ll have to use a little PHP instead:
$query = 'SELECT size FROM album WHERE owner = ' . $user . ';';
$sizes = $facebook->api_client->fql_query($query);
$totalPhotos = 0;
if($sizes){
foreach($sizes as $album){
$totalPhotos += $album['size'];
}
}
?>
The variable totalPhotos will contain the count of all photos at the end, or zero if none were found.
Discussion
As with all FQL queries, this will only return photos that the current loggedinuser has access to, so you won’t be able to query for photos that she can’t see.
Retrieving Five Albums for a User
Problem
I need to retrieve some (or all) of the albums for a specific user from the Facebook Photos app using FQL.
Solution
The simplest form of this query is:
SELECT aid, cover_pid, name, link FROM album WHERE owner = $uid LIMIT 5;
Discussion
When you’re imposing a limit on the data that gets returned, you might want to add an ORDER BY clause so that you’re getting some logical subset. With the case of something like albums, it might make sense to get the five albums most recently updated by a user:
SELECT aid, cover_pid, name, link, modified FROM album
WHERE owner = $uid ORDER BY modified DESC LIMIT 5;
The DESC added to the ORDER BY when using a timestamp as the ordering field will give you results in reverse chronological order (i.e., newest first).
Cookie Table
Problem
What’s the schema for the cookie table?
Solution
The cookie table holds all of the browser cookies that your app has dropped. Its fields are listed in Table 8-3. There’s no way to use this