spacer
Web Development Blog
 Developer Newsletter

Webmaster Blogs
Content & Blogging
Design
Photoshop
General
JavaScript
PHP
PHP Functions
Web
WordPress
Website Promotion

Blog Archives


Developer Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous

Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

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
» Creating facebook like popup dialog in your web app
» Checking leap year - the JS way
» Open Source Social Networking Server: Built on LAMP!
» Intelligent Geocoding using PHP and Yahoo Web Services
» PHP Script execution time and maximum workaround
 


Leave a Reply





About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: