Error in FQL Query - facebook

This is My Code
$ids = join(", ",$man);
if ( !isset($_SESSION[$appID.'_FQLResult']) ) {
$FQLQuery = "SELECT uid, sex, pic_square FROM user WHERE uid in ($ids)";
$FQLResult = $facebook->api(array( 'method' => 'fql.query', 'query' => $FQLQuery, 'access_token'=>$fbme['access_token'] ));
$_SESSION[$appID.'_FQLResult'] = $FQLResult;
} else {
$FQLResult = $_SESSION[$appID.'_FQLResult'];
}
echo $ids;
echo $FQLResult;
There is an error
when i echo $ids it shows the value of $ids but when i echo $FQLResult is shows only "array" written whats wrong in this fql query?
how i can fix this?
I think there is something wrong in this query
$FQLQuery = "SELECT uid, sex, pic_square FROM user WHERE uid in ($ids)";

Use implode() directly instead of join()
After a successful API call, you should be expecting an array, so use print_r($FQLResult);
Always, if you are not sure of the type of the variable use var_dump()
You may want to use the new Graph API end-point (/fql?q=YOUR_QUERY), read here
Serialize your array before adding to sessions
Watch our of the session size limits!

Related

fql query fails - suspected auth issue

In a page, if I make a test fql query it works:
$fql = 'SELECT name from user where uid = ' . $user_id;
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
echo '<pre>Name: ' . $ret_obj[0]['name'] . '</pre>';
However if I try to run this query, it fails:
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex =male';
$ret_obj2 = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql2,
));
echo '<pre>Name: ' . $ret_obj2[0]['name'] . '</pre>';
I suspect it is an auth problem, because the same fql query works in the graph api test tool!
If you want to checkout the full php code, it's the second example on this page:
https://developers.facebook.com/docs/reference/php/facebook-api/
You need just basic permissions for this query, so this is not the issue. Try adding double quotes around "male":
$fql2 = 'SELECT uid, name, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex="male"';
Thanks for the support guys, the query wasn't working for 2 reasons:
1) I forgot the ; after the end of statement
2) In my code I used
`SELECT uid, name, pic_square, FROM`
instead of
`SELECT uid, name, pic_square FROM`

Facebook: Anyone knows why I can't get the event's I'm attending?

I'm trying to get a list of facebook events I'm attending. I have these codes:
$fql = "SELECT eid FROM event_member WHERE uid = 100000266996446 AND rsvp_status = 'attending'";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
foreach( $fqlResult as $keys => $values ){
echo "<div>" . $values['eid'] . "[][]</div>";
}
Many thanks for any help! :)
With your code, please note that you cannot do it if you're using a profile uid. It will only work with fan pages.

fql.get method return empty Array in Facebook Application with facebook-php-sdk-v2.1.2-4

Recently I am trying to develop a facebook application that shows the STATUSES of loged in facebook user.
I am succeeded to retrieve information from user table using FQL. But right now I am using this following code to get data from status table.
****BEGIN :: CODE SECTION***********
$sql = "SELECT message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = ".$myLoginId.")";
$param = array(
// 'method' => 'fql.query',
'method' => 'status.get',
'query' => $sql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
print_r($fqlResult);
***END:: CODE SECTION********
But everytimes it returns Empty Array.
Right Now I am using Library of facebook-php-sdk-v2.1.2-4-g2343fca.tar which I collect from github.com.
can anybody please give me a solution.
Thank in Advance.
You code is working just fine, just a small note, you can use me() to get the current user id:
$fql = "SELECT uid,message,time FROM status WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())";
$fqlResult = $this->facebook->api(array(
'method' => 'fql.query',
'query' => $fql
));
AND once again, the most important part is acquiring the correct permissions, which are user_status (if you want to get your statuses) AND friends_status (to get your friends' statuses).

Facebook New PHP SDK For Graph API - Multi Query

I'm at my wits end in what the queries parameter value should look like. So to sumbit a multiquery accross the graph API library, the following would be the code method to perform it, as far as I can tell.
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
Using this method in the new PHP SDK library from Facebook, has any one made this work? If so can you drop an example on how you build the complete value of the $multiQuery variable?
I've been struggeling with this for a few days and I'm only finding exmaples with the old PHP library.
Why is it always after banging your head for days, you ask a question, and 5 minutes later, you come up with the answer your self.
So here was MY lovely experience.
Since in PHP you can use a "/' character to start a text string, I got my self stuck in the flip flopping of the double quote character and single quote character. It dawned on me that the queries defined in a multi query are, duh, wrapped by double quotes.
So lesson learned? If you have a where clause that uses a string value in a multi query, make sure for pete's sake you use SINGLE QUOTES around the string value your filtering on.
BAD BAD - This is what I did. note the double quotes around myvalue and myothervalue. NAUGHTY!
$multiQuery = {
"query1":"select something from something where somecolumn = "myvalue"",
"query2":"select something from something where somecolumn = "myothervalue""
};
GOOD Example - Now look at myvalue and myothervalue.
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
So now I can...
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
And if any of you are wondering what is the actual type of the $multiQuery variable is (for newbie like me), it's just a string data type. It's not an array, nothing more nifty than text.
Considering an array of node id's with their respective url's as values you'll have
/**
*A JSON-encoded dictionary of the queries to perform. The array contains a set of key/value pairs.
*Each key is a query name, which can contain only alphanumeric characters and optional underscores.
*Each key maps to a value containing a traditional FQL query.
*/
$fql = '{';
foreach ($path as $key1 => $value1) {
$fql .= '"' . $key1 . '":"SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url=\'' . $value1 . '\'",';
}
$fql .= '}';
$param = array(
'method' => 'fql.multiquery',
'queries' => $fql,
'callback' => ''
);
try {
$fqlresult = $facebook->api($param);
} catch (FacebookApiException $e) {
watchdog('Facebook Query', 'Parsing error on node #node | #error', array('#node' => $key1, '#error' => $e), WATCHDOG_DEBUG); }
You can try this:
$multiQuery= array ("query1" => "query #1 goes here","query2" => "query #2 goes here");
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);

FQL: Order photos by likes

Is there anyway to retrieve photos ordered by the number of likes in FQL?
PLEASE NOTE: I have never played with FQL before, but I need this exact solution yesterday, so I gave it a go!
Try adding references to "like_info" in the first line in the previous answer (maybe like_info is new since you asked this?):
$fql = "SELECT like_info, object_id,src_small,link FROM photo WHERE aid = '2389563453799923709' ORDER BY like_info created DESC";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$photos = $facebook->api($param);
This may well give you answers returned in order from most to least liked. At least, it worked for me! :)
Please note: This solution is no longer require as like_info has been added to photo
So, I'm guessing this is impossible to do nicely? Here's the ugly solution:
$fql = "SELECT object_id,src_small,link FROM photo WHERE aid = '2389563453799923709' ORDER BY created DESC";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$photos = $facebook->api($param);
if (count($photos) > 0) {
for ($i = 0; $i < count($photos); $i++) {
$objectId = $photos[$i]['object_id'];
$like_count = $facebook->api('/'.$objectId.'/likes');
$photos[$i]['likes'] = count($like_count['data']);
}
}
function cmp($a, $b) {
if ($a['likes'] == $b['likes'])
return 0;
return $a['likes'] > $b['likes'] ? -1 : 1;
}
usort($photos, 'cmp');