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

Functions and Operators

Problem

What functions and operators are supported in FQL?

Solution

FQL supports a number of functions that will be very familiar to PHP developers (see Table 8-1).

Table 8-1. FQL functions

Function

Description

now()

Returns the current time

rand()

Generates a random number

strlen(string)

Returns the length of the string

concat(string, ...)

Concatenates strings together

substr(string, start, length)

Returns the specified substring

strpos(haystack, needle)

Returns the position of the needle in the haystack

lower(string)

Converts the string to lowercase

upper(string)

Converts the string to uppercase

The standard set of SQL-like operators are supported: =, >=, <, etc., parentheses for order of operations, and the arithmetic operators +, -, *, and /. FQL also supports the AND, OR, and NOT logical operators inside of queries.

Discussion

You can use the functions directly inline with your FQL queries:

SELECT upper(concat(first_name, " ", substr(last_name, 0, 1),
 ".")), birthday FROM user WHERE sex = "female" AND uid IN (SELECT uid2
 FROM friend WHERE uid1 = $user) LIMIT 5;

That may not be the most practical query ever constructed, but it will return user’s first five female friends with their first name and last name’s first initial in all caps. Do with it what you will.

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