用facebook查询结果更新mysql表

时间:2012-12-28 23:30:01

标签: php mysql

我有桌子,我有facebook事件ID。

id    event_id    attendings
 1    12345678

我将把event_id放入查询中:

$query = 'https://graph.facebook.com/fql?q=SELECT+attending_count+FROM+event+WHERE+eid='.$event_id.'&access_token='.$params1['access_token'];
$result = file_get_contents($query);
$obj = json_decode($result, true);
foreach ($obj['data'] as $item) { 
$attend = $item['attending_count'];
} 

现在我将使用$ attend

更新event_id = 12345678的表格

问题是:我如何处理表格中的所有行

由于

对不起我的拼写,英语不是我的母亲。

2 个答案:

答案 0 :(得分:1)

您可以使用单个FQL查询获取所有所需的attending_count值,然后在结果循环内更新现有表:

define('FB_FQL_API', 'https://graph.facebook.com/fql?');

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$qry = $dbh->query('
  SELECT GROUP_CONCAT(event_id)
  FROM   my_table
');

$upd = $dbh->prepare('
  UPDATE my_table
  SET    attendings = :attend
  WHERE  event_id   = :event
');

$fql = '
  SELECT eid, attending_count
  FROM   event
  WHERE  eid IN ('.$qry->fetchColumn().')
';

$url = FB_FQL_API . http_build_query([
  'q'            => trim(preg_replace('/\s+/', ' ', $fql)),
  'access_token' => $params1['access_token']
]);

$res = json_decode(file_get_contents($url));

foreach ($res->data as $event) $upd->execute([
  ':attend' => $event->attending_count,
  ':event'  => $event->eid
]);

答案 1 :(得分:1)

看起来您需要在一个大循环中完成所有操作,因为您必须为每个事件执行facebook API调用。

因此,第一步是使用您选择使用的任何MySQL库从表中获取所有结果。查询将是:

SELECT id, event_id FROM table;

假设您在循环结果集时将数据分配给这样的数组:

$array[$row['id']] = $row['event_id'];

现在,你将像这样循环遍历该数组:

foreach($array as $db_id => $event_id) {
    // your facebook code here

    // update the database with your library of choice using this query
    UPDATE table SET attendings = $attending WHERE id = $db_id
}
相关问题