How can I count the total friends of a Facebook user by uid?

How can I count the total friends of a Facebook user by uid?

Answers


There is a friend_count field against the user table that you can query.

SELECT friend_count FROM user WHERE uid = me()

Just replace me() with the UID of the user that you want to query.


@Scott

This very much works, I didn't expect it to :)

https://graph.facebook.com/fql?q=SELECT friend_count FROM user WHERE uid = 273103345

I tried with a random user id who is not a friend in Graph API. However for few random uids it returned null, I am guessing they were pages.


FQL is now deprecated as of October 2014. Quoting Facebook's FAQ :

As of Tuesday 22nd July 2014, we now return a total_count field within a summary property in the response to /v2.2/me/friends.

The user/friends endpoint does require the user_friends permission to return an actual list of friends who use your app, however the friend count field will still be returned without this the user_friends permission granted. Sample response:

    {
  "data": [
  ], 
  "summary": {
    "total_count": 811
  }
}

Full FAQ: https://developers.facebook.com/docs/apps/faq#total_friend_count


COUNT is not supported by FQL:

SELECT uid2 
FROM friend 
WHERE uid1 = USERID

Since COUNT is not supported you need to get all of the records and then count them in whatever application that is making the SQL call.

If you want to get more information, like names, along with count you can use an inner select to get the friend's extended info:

SELECT name 
FROM user 
WHERE uid IN (SELECT uid2 
              FROM friend 
              WHERE uid1 = USERID)

Here is a related SO question:

FQL result count

References for Friend table:

Query this table to determine whether two users are linked together as friends.


Heres my solution... works great!

$myfriends = $facebook->api('/XXXXXXX152466/friends');
$friendcount =  COUNT($myfriends['data']) + 1;

Replace XXXXXXX152466 with the facebook profile id#

Using Facebook PHP SDK along with Graph API!


Starting with Facebook API 2.0 you can't get the full list of Facebook friends, only a partial list of friends that also authorized your app.

You can still get the total number of friends though, the Graph API user/friends has a field summary containing the count as total_count

So you can get it using:

FB.api("/me/friends", function (response) {
   if (response && !response.error) {
      console.log(response.summary.total_count);
   }
});

There are lot of way to count the no of friends using Facebook Graph API with Php SDK, out of one i am telling..

$fbuser=$fb->api('/me/friends');
$access_token = $fb->getAccessToken();


$friend_count=0;
foreach($fbuser['data'] as $friends)
{
  $friends_count++;
}

For complete tutorial visit this article Getting FB Friends Count


Need Your Help

What is the connection string for odbc connections?

asp.net sql sql-server connection connection-string

I've always done web apps and now I need to do a console app. I need to use both an odbc connection and a regular connection.