O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required