Retrieving a User’s Friends

Problem

I need to retrieve all of a specific user’s friends using FQL.

Solution

This is a really simple query:

SELECT uid2 FROM friend WHERE uid1 = $uid;

Discussion

Luckily for FQL users everywhere, this query returns exactly the same result if you run it the other way around:

SELECT uid1 FROM friend WHERE uid2 = $uid;

You can run this query only when you’re looking for the friends of the current loggedinuser, or you’ll get an “Error 604: Can’t lookup all friends of 12345; only for the logged in user” error (see 600 Errors for more info).

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.