Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:
Webmaster Blog

Unexpected return value from Facebook FQL.query

While working with FQL I suddenly found a bug in one of our applications. Sometime you need to display how many of your friends added this application to their facebook profile. To do that you can execute the following query via the available REST library for PHP developers.

SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})

The PHP code may look like the following one

function getTotalFriends($userid)
{
global $facebook, $mysql;
$Users = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})");
$NumberOfUsers = count(trim($Users));
return $NumberOfUsers;
}

So what would you expect when there is no friend who has added this application? Definitely the FQL query should return an empty array. Number of elements in that array is 0 and you will get an expected result. But actually the method returns a white space. Thats why count(” “) will give you “1″ and produces an unexpected result. Using the above method you will get “1″ when you have no friends.

Here’s the correction


function getTotalFriends($userid)
{
global $facebook, $mysql;
$Users = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})");
if (is_array($Users))
$NumberOfUsers = count(trim($Users));
else
$NumberOfUsers=0;
return $NumberOfUsers;
}


Related Posts
» Checking leap year - the JS way
» Getting Facebook user data, legally
» Creating facebook like popup dialog in your web app
» Open Source Social Networking Server: Built on LAMP!
» MySpace tries to become more habitable
 



Leave a Reply