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:

<?php
$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.

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.